Class: MongoidForums::Permission
- Inherits:
-
Object
- Object
- MongoidForums::Permission
- Defined in:
- app/models/mongoid_forums/permission.rb
Instance Method Summary collapse
- #allow(controllers, actions, &block) ⇒ Object
- #allow?(controller, action, resource = nil) ⇒ Boolean
- #allow_all ⇒ Object
- #allow_nested_param(resources, attribute, nested_attributes) ⇒ Object
- #allow_param(resource, attributes) ⇒ Object
- #allow_param?(resource, attribute) ⇒ Boolean
-
#initialize(user) ⇒ Permission
constructor
A new instance of Permission.
- #permit_params!(params) ⇒ Object
Constructor Details
#initialize(user) ⇒ Permission
Returns a new instance of Permission.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/mongoid_forums/permission.rb', line 4 def initialize(user) allow "mongoid_forums/admin/forums", [:new] allow "mongoid_forums/forums", [:index, :show] allow "mongoid_forums/topics", [:show] do |topic| (topic.hidden && user.present? && topic.user_id == user.id) || !topic.hidden end if user.present? allow "mongoid_forums/posts", [:new, :create] allow "mongoid_forums/forums", [:new, :create] allow "mongoid_forums/redirect", [:forum, :topic, :posts, :subscriptions] allow "mongoid_forums/topics", [:my_subscriptions, :my_topics, :my_posts] allow "mongoid_forums/topics", [:edit, :update] do |topic| topic.user_id == user.id && !topic.locked && !topic.hidden end allow "mongoid_forums/posts", [:edit, :update] do |post| post.user_id == user.id && !post.topic.locked end allow "mongoid_forums/redirect", [:subscriptions] #allow_param :topic, [:name, posts: :text] rank = Rank.where(:members => user.id).first allow_all if rank && rank.admin end end |
Instance Method Details
#allow(controllers, actions, &block) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'app/models/mongoid_forums/permission.rb', line 49 def allow(controllers, actions, &block) @allowed_actions ||= {} Array(controllers).each do |controller| Array(actions).each do |action| @allowed_actions[[controller.to_s, action.to_s]] = block || true end end end |
#allow?(controller, action, resource = nil) ⇒ Boolean
38 39 40 41 42 43 |
# File 'app/models/mongoid_forums/permission.rb', line 38 def allow?(controller, action, resource = nil) puts controller puts action allowed = @allow_all || @allowed_actions[[controller.to_s, action.to_s]] allowed && (allowed == true || resource && allowed.call(resource)) end |
#allow_all ⇒ Object
45 46 47 |
# File 'app/models/mongoid_forums/permission.rb', line 45 def allow_all @allow_all = true end |
#allow_nested_param(resources, attribute, nested_attributes) ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/models/mongoid_forums/permission.rb', line 74 def allow_nested_param(resources, attribute, nested_attributes) @allowed_params ||= {} Array(resources).each do |resource| @allowed_params[resource.to_s] ||= [] @allowed_params[resource.to_s] += [{ attribute.to_s => Array(nested_attributes).map(&:to_s)}] end end |
#allow_param(resource, attributes) ⇒ Object
58 59 60 61 62 63 64 |
# File 'app/models/mongoid_forums/permission.rb', line 58 def allow_param(resource, attributes) @allowed_params ||= {} Array(resource).each do |resource| @allowed_params[resource.to_s] ||= [] @allowed_params[resource.to_s] += Array(attributes).map(&:to_s) end end |
#allow_param?(resource, attribute) ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'app/models/mongoid_forums/permission.rb', line 66 def allow_param?(resource, attribute) if @allow_all true elsif @allowed_params && @allowed_params[resource.to_s] @allowed_params[resource.to_s].include? attribute.to_s end end |
#permit_params!(params) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/mongoid_forums/permission.rb', line 82 def permit_params!(params) if @allow_all params.permit! elsif @allowed_params @allowed_params.each do |resource, attributes| if params[resource].respond_to? :permit params[resource] = params[resource].permit(*attributes) end end end end |