1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
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.
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.
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.
18 class Permission < ActiveRecord::Base
19 has_and_belongs_to_many :roles
21 validates_presence_of :controller, :action, :description
24 100 => :label_project,
25 200 => :label_member_plural,
26 300 => :label_version_plural,
27 400 => :label_issue_category_plural,
28 600 => :label_query_plural,
29 1000 => :label_issue_plural,
30 1100 => :label_news_plural,
31 1200 => :label_document_plural,
32 1300 => :label_attachment_plural,
33 1400 => :label_repository
36 @@cached_perms_for_public = nil
37 @@cached_perms_for_roles = nil
40 self.controller + "/" + self.action
47 def self.allowed_to_public(action)
48 @@cached_perms_for_public ||= find(:all, :conditions => ["is_public=?", true]).collect {|p| "#{p.controller}/#{p.action}"}
49 @@cached_perms_for_public.include? action
52 def self.allowed_to_role(action, role)
53 @@cached_perms_for_roles ||=
56 find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
59 allowed_to_public(action) or (@@cached_perms_for_roles[action] and @@cached_perms_for_roles[action].include? role)
62 def self.allowed_to_role_expired
63 @@cached_perms_for_roles = nil