Class: Role

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/role.rb

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

See doc/COPYRIGHT.rdoc for more details. ++

Constant Summary collapse

BUILTIN_NON_MEMBER =

Built-in roles

1
BUILTIN_ANONYMOUS =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.anonymousObject

Return the builtin 'anonymous' role. If the role doesn't exist, it will be created on the fly.


134
135
136
137
138
139
140
141
142
143
# File 'app/models/role.rb', line 134

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
end

.find_all_givableObject

Find all the roles that can be given to a project member


115
116
117
# File 'app/models/role.rb', line 115

def self.find_all_givable
  find(:all, :conditions => {:builtin => 0}, :order => 'position')
end

.non_memberObject

Return the builtin 'non member' role. If the role doesn't exist, it will be created on the fly.


121
122
123
124
125
126
127
128
129
130
# File 'app/models/role.rb', line 121

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
end

Instance Method Details

#<=>(role) ⇒ Object


76
77
78
# File 'app/models/role.rb', line 76

def <=>(role)
  role ? position <=> role.position : -1
end

#add_permission!(*perms) ⇒ Object


53
54
55
56
57
58
59
60
61
62
# File 'app/models/role.rb', line 53

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

  permissions_will_change!
  perms.each do |p|
    p = p.to_sym
    permissions << p unless permissions.include?(p)
  end
  save!
end

#allowed_to?(action) ⇒ Boolean

Return true if role is allowed to do the specified action action can be:

  • a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')

  • a permission Symbol (eg. :edit_project)

Returns:

  • (Boolean)

98
99
100
101
102
103
104
# File 'app/models/role.rb', line 98

def allowed_to?(action)
  if action.is_a? Hash
    allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
  else
    allowed_permissions.include? action
  end
end

#builtin?Boolean

Return true if the role is a builtin role

Returns:

  • (Boolean)

85
86
87
# File 'app/models/role.rb', line 85

def builtin?
  self.builtin != 0
end

#has_permission?(perm) ⇒ Boolean

Returns true if the role has the given permission

Returns:

  • (Boolean)

72
73
74
# File 'app/models/role.rb', line 72

def has_permission?(perm)
  !permissions.nil? && permissions.include?(perm.to_sym)
end

#member?Boolean

Return true if the role is a project member role

Returns:

  • (Boolean)

90
91
92
# File 'app/models/role.rb', line 90

def member?
  !self.builtin?
end

#permissionsObject


44
45
46
# File 'app/models/role.rb', line 44

def permissions
  read_attribute(:permissions) || []
end

#permissions=(perms) ⇒ Object


48
49
50
51
# File 'app/models/role.rb', line 48

def permissions=(perms)
  perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
  write_attribute(:permissions, perms)
end

#remove_permission!(*perms) ⇒ Object


64
65
66
67
68
69
# File 'app/models/role.rb', line 64

def remove_permission!(*perms)
  return unless permissions.is_a?(Array)
  permissions_will_change!
  perms.each { |p| permissions.delete(p.to_sym) }
  save!
end

#setable_permissionsObject

Return all the permissions that can be given to the role


107
108
109
110
111
112
# File 'app/models/role.rb', line 107

def setable_permissions
  setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
  setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
  setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
  setable_permissions
end

#to_sObject


80
81
82
# File 'app/models/role.rb', line 80

def to_s
  name
end