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 / lib / redmine / access_control.rb @ 912:5e80956cc792

History | View | Annotate | Download (3.34 KB)

1 909:cbb26bc654de Chris
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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
module Redmine
19
  module AccessControl
20 909:cbb26bc654de Chris
21 0:513646585e45 Chris
    class << self
22
      def map
23
        mapper = Mapper.new
24
        yield mapper
25
        @permissions ||= []
26
        @permissions += mapper.mapped_permissions
27
      end
28 909:cbb26bc654de Chris
29 0:513646585e45 Chris
      def permissions
30
        @permissions
31
      end
32 909:cbb26bc654de Chris
33 0:513646585e45 Chris
      # Returns the permission of given name or nil if it wasn't found
34
      # Argument should be a symbol
35
      def permission(name)
36
        permissions.detect {|p| p.name == name}
37
      end
38 909:cbb26bc654de Chris
39 0:513646585e45 Chris
      # Returns the actions that are allowed by the permission of given name
40
      def allowed_actions(permission_name)
41
        perm = permission(permission_name)
42
        perm ? perm.actions : []
43
      end
44 909:cbb26bc654de Chris
45 0:513646585e45 Chris
      def public_permissions
46
        @public_permissions ||= @permissions.select {|p| p.public?}
47
      end
48 909:cbb26bc654de Chris
49 0:513646585e45 Chris
      def members_only_permissions
50
        @members_only_permissions ||= @permissions.select {|p| p.require_member?}
51
      end
52 909:cbb26bc654de Chris
53 0:513646585e45 Chris
      def loggedin_only_permissions
54
        @loggedin_only_permissions ||= @permissions.select {|p| p.require_loggedin?}
55
      end
56 909:cbb26bc654de Chris
57 0:513646585e45 Chris
      def available_project_modules
58
        @available_project_modules ||= @permissions.collect(&:project_module).uniq.compact
59
      end
60 909:cbb26bc654de Chris
61 0:513646585e45 Chris
      def modules_permissions(modules)
62
        @permissions.select {|p| p.project_module.nil? || modules.include?(p.project_module.to_s)}
63
      end
64
    end
65 909:cbb26bc654de Chris
66 0:513646585e45 Chris
    class Mapper
67
      def initialize
68
        @project_module = nil
69
      end
70 909:cbb26bc654de Chris
71 0:513646585e45 Chris
      def permission(name, hash, options={})
72
        @permissions ||= []
73
        options.merge!(:project_module => @project_module)
74
        @permissions << Permission.new(name, hash, options)
75
      end
76 909:cbb26bc654de Chris
77 0:513646585e45 Chris
      def project_module(name, options={})
78
        @project_module = name
79
        yield self
80
        @project_module = nil
81
      end
82 909:cbb26bc654de Chris
83 0:513646585e45 Chris
      def mapped_permissions
84
        @permissions
85
      end
86
    end
87 909:cbb26bc654de Chris
88 0:513646585e45 Chris
    class Permission
89
      attr_reader :name, :actions, :project_module
90 909:cbb26bc654de Chris
91 0:513646585e45 Chris
      def initialize(name, hash, options)
92
        @name = name
93
        @actions = []
94
        @public = options[:public] || false
95
        @require = options[:require]
96
        @project_module = options[:project_module]
97
        hash.each do |controller, actions|
98
          if actions.is_a? Array
99
            @actions << actions.collect {|action| "#{controller}/#{action}"}
100
          else
101
            @actions << "#{controller}/#{actions}"
102
          end
103
        end
104
        @actions.flatten!
105
      end
106 909:cbb26bc654de Chris
107 0:513646585e45 Chris
      def public?
108
        @public
109
      end
110 909:cbb26bc654de Chris
111 0:513646585e45 Chris
      def require_member?
112
        @require && @require == :member
113
      end
114 909:cbb26bc654de Chris
115 0:513646585e45 Chris
      def require_loggedin?
116
        @require && (@require == :member || @require == :loggedin)
117
      end
118 909:cbb26bc654de Chris
    end
119 0:513646585e45 Chris
  end
120
end