From cf56698d91cedb461726200cf0e32834e7124cda Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 20 Jul 2011 16:34:55 +0000 Subject: [PATCH] Refactor builtin roles creation. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6299 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/role.rb | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/app/models/role.rb b/app/models/role.rb index c11111c0a..4000d645e 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -139,31 +139,17 @@ class Role < ActiveRecord::Base # Return the builtin 'non member' role. If the role doesn't exist, # it will be created on the fly. def self.non_member - non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) - if non_member_role.nil? - non_member_role = create(:name => 'Non member', :position => 0) do |role| - role.builtin = BUILTIN_NON_MEMBER - end - raise 'Unable to create the non-member role.' if non_member_role.new_record? - end - non_member_role + find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member') end # Return the builtin 'anonymous' role. If the role doesn't exist, # it will be created on the fly. def self.anonymous - anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) - if anonymous_role.nil? - anonymous_role = create(:name => 'Anonymous', :position => 0) do |role| - role.builtin = BUILTIN_ANONYMOUS - end - raise 'Unable to create the anonymous role.' if anonymous_role.new_record? - end - anonymous_role + find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous') end - private + def allowed_permissions @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} end @@ -176,4 +162,15 @@ private raise "Can't delete role" if members.any? raise "Can't delete builtin role" if builtin? end + + def self.find_or_create_system_role(builtin, name) + role = first(:conditions => {:builtin => builtin}) + if role.nil? + role = create(:name => name, :position => 0) do |r| + r.builtin = builtin + end + raise "Unable to create the #{name} role." if role.new_record? + end + role + end end