To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / role.rb @ 440:6253d777aa12

History | View | Annotate | Download (5.02 KB)

1
# redMine - project management software
2
# Copyright (C) 2006  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

    
18
class Role < ActiveRecord::Base
19
  # Built-in roles
20
  BUILTIN_NON_MEMBER = 1
21
  BUILTIN_ANONYMOUS  = 2
22

    
23
  named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
24
  named_scope :builtin, lambda { |*args|
25
    compare = 'not' if args.first == true
26
    { :conditions => "#{compare} builtin = 0" }
27
  }
28
  
29
  before_destroy :check_deletable
30
  has_many :workflows, :dependent => :delete_all do
31
    def copy(source_role)
32
      Workflow.copy(nil, source_role, nil, proxy_owner)
33
    end
34
  end
35
  
36
  has_many :member_roles, :dependent => :destroy
37
  has_many :members, :through => :member_roles
38
  acts_as_list
39
  
40
  serialize :permissions, Array
41
  attr_protected :builtin
42

    
43
  validates_presence_of :name
44
  validates_uniqueness_of :name
45
  validates_length_of :name, :maximum => 30
46
  validates_format_of :name, :with => /^[\w\s\'\-]*$/i
47

    
48
  def permissions
49
    read_attribute(:permissions) || []
50
  end
51
  
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

    
57
  def add_permission!(*perms)
58
    self.permissions = [] unless permissions.is_a?(Array)
59

    
60
    permissions_will_change!
61
    perms.each do |p|
62
      p = p.to_sym
63
      permissions << p unless permissions.include?(p)
64
    end
65
    save!
66
  end
67

    
68
  def remove_permission!(*perms)
69
    return unless permissions.is_a?(Array)
70
    permissions_will_change!
71
    perms.each { |p| permissions.delete(p.to_sym) }
72
    save!
73
  end
74
  
75
  # Returns true if the role has the given permission
76
  def has_permission?(perm)
77
    !permissions.nil? && permissions.include?(perm.to_sym)
78
  end
79
  
80
  def <=>(role)
81
    role ? position <=> role.position : -1
82
  end
83
  
84
  def to_s
85
    name
86
  end
87
  
88
  # Return true if the role is a builtin role
89
  def builtin?
90
    self.builtin != 0
91
  end
92
  
93
  # Return true if the role is a project member role
94
  def member?
95
    !self.builtin?
96
  end
97
  
98
  # Return true if role is allowed to do the specified action
99
  # action can be:
100
  # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
101
  # * a permission Symbol (eg. :edit_project)
102
  def allowed_to?(action)
103
    if action.is_a? Hash
104
      allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
105
    else
106
      allowed_permissions.include? action
107
    end
108
  end
109
  
110
  # Return all the permissions that can be given to the role
111
  def setable_permissions
112
    setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
113
    setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
114
    setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
115
    setable_permissions
116
  end
117

    
118
  # Find all the roles that can be given to a project member
119
  def self.find_all_givable
120
    find(:all, :conditions => {:builtin => 0}, :order => 'position')
121
  end
122

    
123
  # Return the builtin 'non member' role.  If the role doesn't exist,
124
  # it will be created on the fly.
125
  def self.non_member
126
    non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER})
127
    if non_member_role.nil?
128
      non_member_role = create(:name => 'Non member', :position => 0) do |role|
129
        role.builtin = BUILTIN_NON_MEMBER
130
      end
131
      raise 'Unable to create the non-member role.' if non_member_role.new_record?
132
    end
133
    non_member_role
134
  end
135

    
136
  # Return the builtin 'anonymous' role.  If the role doesn't exist,
137
  # it will be created on the fly.
138
  def self.anonymous
139
    anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS})
140
    if anonymous_role.nil?
141
      anonymous_role = create(:name => 'Anonymous', :position => 0) do |role|
142
        role.builtin = BUILTIN_ANONYMOUS
143
      end
144
      raise 'Unable to create the anonymous role.' if anonymous_role.new_record?
145
    end
146
    anonymous_role
147
  end
148

    
149
  
150
private
151
  def allowed_permissions
152
    @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
153
  end
154

    
155
  def allowed_actions
156
    @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
157
  end
158
    
159
  def check_deletable
160
    raise "Can't delete role" if members.any?
161
    raise "Can't delete builtin role" if builtin?
162
  end
163
end