You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

role.rb 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Role < ActiveRecord::Base
  18. # Built-in roles
  19. BUILTIN_NON_MEMBER = 1
  20. BUILTIN_ANONYMOUS = 2
  21. ISSUES_VISIBILITY_OPTIONS = [
  22. ['all', :label_issues_visibility_all],
  23. ['default', :label_issues_visibility_public],
  24. ['own', :label_issues_visibility_own]
  25. ]
  26. named_scope :sorted, {:order => 'builtin, position'}
  27. named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
  28. named_scope :builtin, lambda { |*args|
  29. compare = 'not' if args.first == true
  30. { :conditions => "#{compare} builtin = 0" }
  31. }
  32. before_destroy :check_deletable
  33. has_many :workflows, :dependent => :delete_all do
  34. def copy(source_role)
  35. Workflow.copy(nil, source_role, nil, proxy_association.owner)
  36. end
  37. end
  38. has_many :member_roles, :dependent => :destroy
  39. has_many :members, :through => :member_roles
  40. acts_as_list
  41. serialize :permissions, Array
  42. attr_protected :builtin
  43. validates_presence_of :name
  44. validates_uniqueness_of :name
  45. validates_length_of :name, :maximum => 30
  46. validates_inclusion_of :issues_visibility,
  47. :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
  48. :if => lambda {|role| role.respond_to?(:issues_visibility)}
  49. def permissions
  50. read_attribute(:permissions) || []
  51. end
  52. def permissions=(perms)
  53. perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
  54. write_attribute(:permissions, perms)
  55. end
  56. def add_permission!(*perms)
  57. self.permissions = [] unless permissions.is_a?(Array)
  58. permissions_will_change!
  59. perms.each do |p|
  60. p = p.to_sym
  61. permissions << p unless permissions.include?(p)
  62. end
  63. save!
  64. end
  65. def remove_permission!(*perms)
  66. return unless permissions.is_a?(Array)
  67. permissions_will_change!
  68. perms.each { |p| permissions.delete(p.to_sym) }
  69. save!
  70. end
  71. # Returns true if the role has the given permission
  72. def has_permission?(perm)
  73. !permissions.nil? && permissions.include?(perm.to_sym)
  74. end
  75. def <=>(role)
  76. role ? position <=> role.position : -1
  77. end
  78. def to_s
  79. name
  80. end
  81. def name
  82. case builtin
  83. when 1; l(:label_role_non_member, :default => read_attribute(:name))
  84. when 2; l(:label_role_anonymous, :default => read_attribute(:name))
  85. else; read_attribute(:name)
  86. end
  87. end
  88. # Return true if the role is a builtin role
  89. def builtin?
  90. self.builtin != 0
  91. end
  92. # Return true if the role is a project member role
  93. def member?
  94. !self.builtin?
  95. end
  96. # Return true if role is allowed to do the specified action
  97. # action can be:
  98. # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
  99. # * a permission Symbol (eg. :edit_project)
  100. def allowed_to?(action)
  101. if action.is_a? Hash
  102. allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
  103. else
  104. allowed_permissions.include? action
  105. end
  106. end
  107. # Return all the permissions that can be given to the role
  108. def setable_permissions
  109. setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
  110. setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
  111. setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
  112. setable_permissions
  113. end
  114. # Find all the roles that can be given to a project member
  115. def self.find_all_givable
  116. find(:all, :conditions => {:builtin => 0}, :order => 'position')
  117. end
  118. # Return the builtin 'non member' role. If the role doesn't exist,
  119. # it will be created on the fly.
  120. def self.non_member
  121. find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
  122. end
  123. # Return the builtin 'anonymous' role. If the role doesn't exist,
  124. # it will be created on the fly.
  125. def self.anonymous
  126. find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
  127. end
  128. private
  129. def allowed_permissions
  130. @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
  131. end
  132. def allowed_actions
  133. @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
  134. end
  135. def check_deletable
  136. raise "Can't delete role" if members.any?
  137. raise "Can't delete builtin role" if builtin?
  138. end
  139. def self.find_or_create_system_role(builtin, name)
  140. role = first(:conditions => {:builtin => builtin})
  141. if role.nil?
  142. role = create(:name => name, :position => 0) do |r|
  143. r.builtin = builtin
  144. end
  145. raise "Unable to create the #{name} role." if role.new_record?
  146. end
  147. role
  148. end
  149. end