]> git.openstreetmap.org Git - rails.git/blob - app/models/ability.rb
Implement the cancan filters for diary entries
[rails.git] / app / models / ability.rb
1 # frozen_string_literal: true
2
3 class Ability
4   include CanCan::Ability
5
6   def initialize(user, token)
7     can :index, :site
8     can [:permalink, :edit, :help, :fixthemap, :offline, :export, :about, :preview, :copyright, :key, :id, :welcome], :site
9
10     can [:list, :rss, :view, :comments], DiaryEntry
11
12     if user
13       can :weclome, :site
14
15       can [:create, :edit, :comment, :subscribe, :unsubscribe], DiaryEntry
16
17       if user.administrator?
18         can [:hide, :hidecomment], [DiaryEntry, DiaryComment]
19       end
20     end
21     # Define abilities for the passed in user here. For example:
22     #
23     #   user ||= User.new # guest user (not logged in)
24     #   if user.admin?
25     #     can :manage, :all
26     #   else
27     #     can :read, :all
28     #   end
29     #
30     # The first argument to `can` is the action you are giving the user
31     # permission to do.
32     # If you pass :manage it will apply to every action. Other common actions
33     # here are :read, :create, :update and :destroy.
34     #
35     # The second argument is the resource the user can perform the action on.
36     # If you pass :all it will apply to every resource. Otherwise pass a Ruby
37     # class of the resource.
38     #
39     # The third argument is an optional hash of conditions to further filter the
40     # objects.
41     # For example, here the user can only update published articles.
42     #
43     #   can :update, Article, :published => true
44     #
45     # See the wiki for details:
46     # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
47   end
48
49   def has_capability?(token, cap)
50     token && token.read_attribute(cap)
51   end
52 end