OSDN Git Service

added svn:eol-style native property on /app files
[redminele/redmine.git] / app / models / permission.rb
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 Permission < ActiveRecord::Base
19   has_and_belongs_to_many :roles
20
21   validates_presence_of :controller, :action, :description
22
23   GROUPS = {
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
34   }.freeze
35   
36   @@cached_perms_for_public = nil
37   @@cached_perms_for_roles = nil
38   
39   def name
40     self.controller + "/" + self.action
41   end
42   
43   def group_id
44     (self.sort / 100)*100
45   end
46   
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
50   end
51   
52   def self.allowed_to_role(action, role)
53     @@cached_perms_for_roles ||=
54       begin
55         perms = {}
56         find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
57         perms
58       end
59     allowed_to_public(action) or (@@cached_perms_for_roles[action] and @@cached_perms_for_roles[action].include? role)
60   end
61   
62   def self.allowed_to_role_expired
63     @@cached_perms_for_roles = nil
64   end
65 end