Class: EnjuLibrary::Ability

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

Instance Method Summary collapse

Constructor Details

#initialize(user, ip_address = 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
# File 'app/models/enju_library/ability.rb', line 5

def initialize(user, ip_address = nil)
  case user.try(:role).try(:name)
  when 'Administrator'
    can [:read, :create, :update], Bookstore
    can [:delete, :destroy], Bookstore do |bookstore|
      bookstore.items.empty?
    end
    can [:read, :create, :update], Library
    can [:delete, :destroy], Library do |library|
      if library.profiles.empty?
        true if library.shelves.empty? && !library.web?
      end
    end
    can [:read, :create, :update], Shelf
    can [:delete, :destroy], Shelf do |shelf|
      shelf.items.empty? && !shelf.web_shelf?
    end
    can :manage, [
      Accept,
      BudgetType,
      SearchEngine,
      Subscribe,
      Subscription,
      Withdraw
    ]
    can :update, [
      LibraryGroup,
      RequestStatusType,
      RequestType
    ] if LibraryGroup.site_config.network_access_allowed?(ip_address, network_type: :admin)
    can :read, [
      LibraryGroup,
      RequestStatusType,
      RequestType
    ]
  when 'Librarian'
    can :manage, [
      Accept,
      Subscribe,
      Subscription,
      Withdraw
    ]
    can :read, [
      Bookstore,
      BudgetType,
      Library,
      LibraryGroup,
      RequestStatusType,
      RequestType,
      SearchEngine,
      Shelf
    ]
  when 'User'
    can :read, [
      Library,
      LibraryGroup,
      Shelf
    ]
  else
    can :read, [
      Library,
      LibraryGroup,
      Shelf
    ]
  end
end