Class: CamaleonCms::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/camaleon_cms/ability.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, current_site = nil) ⇒ Ability

Returns a new instance of Ability.



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/camaleon_cms/ability.rb', line 5

def initialize(user, current_site = nil)
  # Define abilities for the passed in user here. For example:
  #
  user ||= CamaleonCms::User.new # guest user (not logged in)
  if user.admin?
    can :manage, :all
  elsif user.client?
    can :read, :all
  else
    # conditions:
    # Fetch the role record fresh from the database for the current site to
    # ensure up-to-date role meta (avoid stale cached role objects during
    # tests or runtime meta updates).
    current_user_role = if current_site.present?
                          current_site.user_roles.where(slug: user.role).first
                        else
                          user.get_role(current_site)
                        end || current_site.user_roles.new
    @roles_manager = current_user_role.get_meta("_manager_#{current_site.id}", {}) || {}
    @roles_post_type = current_user_role.get_meta("_post_type_#{current_site.id}", {}) || {}

    ids_publish = @roles_post_type[:publish] || []
    ids_edit = @roles_post_type[:edit] || []
    ids_edit_other = @roles_post_type[:edit_other] || []
    ids_edit_publish = @roles_post_type[:edit_publish] || []
    ids_delete = @roles_post_type[:delete] || []
    ids_delete_other = @roles_post_type[:delete_other] || []
    ids_delete_publish = @roles_post_type[:delete_publish] || []

    safe_can :posts, CamaleonCms::PostType do |pt|
      (ids_edit + ids_edit_other + ids_edit_publish).to_i.include?(pt.id)
    end

    safe_can :create_post, CamaleonCms::PostType do |pt|
      ids_edit.to_i.include?(pt.id)
    end
    safe_can :publish_post, CamaleonCms::PostType do |pt|
      ids_publish.to_i.include?(pt.id)
    end
    safe_can :edit_other, CamaleonCms::PostType do |pt|
      ids_edit_other.to_i.include?(pt.id)
    end
    safe_can :edit_publish, CamaleonCms::PostType do |pt|
      ids_edit_publish.to_i.include?(pt.id)
    end

    safe_can :categories, CamaleonCms::PostType do |pt|
      @roles_post_type[:manage_categories].to_i.include?(pt.id)
    end
    safe_can :post_tags, CamaleonCms::PostType do |pt|
      @roles_post_type[:manage_tags].to_i.include?(pt.id)
    end

    safe_can :update, CamaleonCms::Post do |post|
      pt_id = post.post_type.id
      r = false
      r ||= ids_edit.to_i.include?(pt_id) && post.user_id == user.id
      r ||= ids_edit_publish.to_i.include?(pt_id) && post.published?
      r ||= ids_edit_other.to_i.include?(pt_id) && post.user_id != user.id
      r
    end

    safe_can :destroy, CamaleonCms::Post do |post|
      pt_id = post.post_type.id
      r = false
      r ||= ids_delete.to_i.include?(pt_id) && post.user_id == user.id
      r ||= ids_delete_publish.to_i.include?(pt_id) && post.published?
      r ||= ids_delete_other.to_i.include?(pt_id) && post.user_id != user.id
      r
    end

    # support for custom abilities for each posttype
    # sample: https://camaleon.website/documentation/category/40756-uncategorized/custom-models.html
    @roles_post_type.each do |k, v|
      next if %w[edit edit_other edit_publish publish manage_categories].include?(k.to_s)

      safe_can k.to_sym, CamaleonCms::PostType do |pt|
        v.include?(pt.id.to_s)
      end
    end

    # others
    %i[media comments themes widgets nav_menu plugins users settings custom_fields select_eval].each do |manager_key|
      safe_can :manage, manager_key if @roles_manager[manager_key]
    end
    @roles_manager.try(:each) do |rol_manage_key, val_role|
      safe_can :manage, rol_manage_key.to_sym if val_role.to_s.cama_true?
    end
  end
  cannot :impersonate, CamaleonCms::User do |u|
    u.id == user.id
  end
end

Instance Method Details

#can?(action, subject, *extra_args) ⇒ Boolean

overwrite can method to support decorator class names

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'app/models/camaleon_cms/ability.rb', line 100

def can?(action, subject, *extra_args)
  if subject.is_a?(Draper::Decorator)
    super(action, subject.model, *extra_args)
  else
    super(action, subject, *extra_args)
  end
end

#cannot?(*args) ⇒ Boolean

overwrite cannot method to support decorator class names

Returns:

  • (Boolean)


109
110
111
# File 'app/models/camaleon_cms/ability.rb', line 109

def cannot?(*args)
  !can?(*args)
end