##// END OF EJS Templates
Refactor builtin roles creation....
Jean-Philippe Lang -
r6179:cf56698d91ce
parent child
Show More
@@ -139,31 +139,17 class Role < ActiveRecord::Base
139 # Return the builtin 'non member' role. If the role doesn't exist,
139 # Return the builtin 'non member' role. If the role doesn't exist,
140 # it will be created on the fly.
140 # it will be created on the fly.
141 def self.non_member
141 def self.non_member
142 non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER})
142 find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
143 if non_member_role.nil?
144 non_member_role = create(:name => 'Non member', :position => 0) do |role|
145 role.builtin = BUILTIN_NON_MEMBER
146 end
147 raise 'Unable to create the non-member role.' if non_member_role.new_record?
148 end
149 non_member_role
150 end
143 end
151
144
152 # Return the builtin 'anonymous' role. If the role doesn't exist,
145 # Return the builtin 'anonymous' role. If the role doesn't exist,
153 # it will be created on the fly.
146 # it will be created on the fly.
154 def self.anonymous
147 def self.anonymous
155 anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS})
148 find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
156 if anonymous_role.nil?
157 anonymous_role = create(:name => 'Anonymous', :position => 0) do |role|
158 role.builtin = BUILTIN_ANONYMOUS
159 end
160 raise 'Unable to create the anonymous role.' if anonymous_role.new_record?
161 end
162 anonymous_role
163 end
149 end
164
165
150
166 private
151 private
152
167 def allowed_permissions
153 def allowed_permissions
168 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
154 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
169 end
155 end
@@ -176,4 +162,15 private
176 raise "Can't delete role" if members.any?
162 raise "Can't delete role" if members.any?
177 raise "Can't delete builtin role" if builtin?
163 raise "Can't delete builtin role" if builtin?
178 end
164 end
165
166 def self.find_or_create_system_role(builtin, name)
167 role = first(:conditions => {:builtin => builtin})
168 if role.nil?
169 role = create(:name => name, :position => 0) do |r|
170 r.builtin = builtin
171 end
172 raise "Unable to create the #{name} role." if role.new_record?
173 end
174 role
175 end
179 end
176 end
General Comments 0
You need to be logged in to leave comments. Login now