You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

permission.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. class Permission < ActiveRecord::Base
  18. has_and_belongs_to_many :roles
  19. validates_presence_of :controller, :action, :description
  20. GROUPS = {
  21. 100 => :label_project,
  22. 200 => :label_member_plural,
  23. 300 => :label_version_plural,
  24. 400 => :label_issue_category_plural,
  25. 1000 => :label_issue_plural,
  26. 1100 => :label_news_plural,
  27. 1200 => :label_document_plural,
  28. 1300 => :label_attachment_plural,
  29. }.freeze
  30. @@cached_perms_for_public = nil
  31. @@cached_perms_for_roles = nil
  32. def name
  33. self.controller + "/" + self.action
  34. end
  35. def group_id
  36. (self.sort / 100)*100
  37. end
  38. def self.allowed_to_public(action)
  39. @@cached_perms_for_public ||= find(:all, :conditions => ["is_public=?", true]).collect {|p| "#{p.controller}/#{p.action}"}
  40. @@cached_perms_for_public.include? action
  41. end
  42. def self.allowed_to_role(action, role)
  43. @@cached_perms_for_roles ||=
  44. begin
  45. perms = {}
  46. find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
  47. perms
  48. end
  49. @@cached_perms_for_roles[action] and @@cached_perms_for_roles[action].include? role
  50. end
  51. def self.allowed_to_role_expired
  52. @@cached_perms_for_roles = nil
  53. end
  54. end