Class: EnjuBiblio::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/enju_biblio/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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/models/enju_biblio/ability.rb', line 5

def initialize(user, ip_address = nil)
  case user.try(:role).try(:name)
  when 'Administrator'
    can [:read, :create, :update], CarrierType
    can [:destroy, :delete], CarrierType do |carrier_type|
      true unless carrier_type.manifestations.exists?
    end if LibraryGroup.site_config.network_access_allowed?(ip_address)
    can [:read, :create, :update], IdentifierType
    can [:destroy, :delete], IdentifierType do |identifier_type|
      true unless identifier_type.identifiers.exists?
    end if LibraryGroup.site_config.network_access_allowed?(ip_address)
    can [:read, :create, :update], Item
    can [:destroy, :delete], Item do |item|
      item.removable?
    end
    can [:read, :create, :update], Manifestation
    can [:destroy, :delete], Manifestation do |manifestation|
      if  manifestation.items.empty?
        if manifestation.series_master?
          if manifestation.children.empty?
            true
          else
            false
          end
        else
          true
        end
      end
    end
    can :manage, [
      AgentMerge,
      AgentMergeList,
      Create,
      CreateType,
      Donate,
      Identifier,
      ImportRequest,
      ManifestationRelationship,
      ManifestationRelationshipType,
      Own,
      Agent,
      AgentImportFile,
      AgentRelationship,
      AgentRelationshipType,
      FormOfWork,
      PictureFile,
      Produce,
      ProduceType,
      Realize,
      RealizeType,
      ResourceImportFile,
      ResourceExportFile,
      SeriesStatement,
      SeriesStatementMerge,
      SeriesStatementMergeList
    ]
    can :manage, [
      Country,
      Language,
      License,
      MediumOfPerformance
    ]
    can [:destroy, :delete], [
      ContentType,
      Frequency
    ] do |record|
      record.manifestations.empty?
    end if LibraryGroup.site_config.network_access_allowed?(ip_address)
    can [:destroy, :delete], [
      AgentType
    ] do |record|
      record.agents.empty?
    end if LibraryGroup.site_config.network_access_allowed?(ip_address)
    can [:create, :read, :update], [
      AgentType,
      AgentImportResult,
      CarrierType,
      ContentType,
      Country,
      Frequency,
      FormOfWork,
      IdentifierType,
      Language,
      License,
      MediumOfPerformance,
      ResourceImportResult
    ]
  when 'Librarian'
    can :manage, Item
    can [:destroy, :delete], Item do |item|
      item.removable?
    end
    can [:index, :create], Manifestation
    can [:show, :update], Manifestation do |manifestation|
      manifestation.required_role_id <= 3
    end
    can [:destroy, :delete], Manifestation do |manifestation|
      if manifestation.required_role_id <= 3
        if manifestation.items.empty?
          if manifestation.series_master?
            if manifestation.children.empty?
              true
            else
              false
            end
          else
            true
          end
        end
      else
        false
      end
    end
    can [:index, :create], Agent
    can :show, Agent do |agent|
      agent.required_role_id <= 3
    end
    can [:update, :destroy, :delete], Agent do |agent|
      agent.required_role_id <= 3
    end
    can :manage, [
      AgentMerge,
      AgentMergeList,
      Create,
      Donate,
      Identifier,
      ImportRequest,
      ManifestationRelationship,
      Own,
      AgentImportFile,
      AgentRelationship,
      PictureFile,
      Produce,
      Realize,
      ResourceImportFile,
      ResourceExportFile,
      SeriesStatement,
      SeriesStatementMerge,
      SeriesStatementMergeList
    ]
    can :read, [
      AgentType,
      AgentImportResult,
      AgentRelationshipType,
      CarrierType,
      ContentType,
      Country,
      Frequency,
      FormOfWork,
      IdentifierType,
      Language,
      License,
      ManifestationRelationshipType,
      MediumOfPerformance,
      ResourceImportResult
    ]
  when 'User'
    can :index, Item
    can :show, Item do |item|
      item.required_role_id <= 2
    end
    can :index, Manifestation
    can [:show, :edit], Manifestation do |manifestation|
      manifestation.required_role_id <= 2
    end
    can :index, Agent
    can :show, Agent do |agent|
      true if agent.required_role_id <= 2 #name == 'Administrator'
    end
    can :index, PictureFile
    can :show, PictureFile do |picture_file|
      begin
        true if picture_file.picture_attachable.required_role_id <= 2
      rescue NoMethodError
        true
      end
    end
    can :read, [
      CarrierType,
      ContentType,
      Country,
      Create,
      Frequency,
      FormOfWork,
      Identifier,
      IdentifierType,
      Language,
      License,
      ManifestationRelationship,
      ManifestationRelationshipType,
      MediumOfPerformance,
      Own,
      AgentRelationship,
      AgentRelationshipType,
      Produce,
      Realize,
      SeriesStatement
    ]
  else
    can :index, Manifestation
    can :show, Manifestation do |manifestation|
      manifestation.required_role_id == 1
    end
    can :index, Agent
    can :show, Agent do |agent|
      agent.required_role_id == 1 #name == 'Guest'
    end
    can :read, [
      CarrierType,
      ContentType,
      Country,
      Create,
      Frequency,
      FormOfWork,
      Identifier,
      IdentifierType,
      Item,
      Language,
      License,
      ManifestationRelationship,
      ManifestationRelationshipType,
      MediumOfPerformance,
      Own,
      AgentRelationship,
      AgentRelationshipType,
      PictureFile,
      Produce,
      Realize,
      SeriesStatement
    ]
  end
end