Class: Ability
- Inherits:
-
Object
- Object
- Ability
- Includes:
- CanCan::Ability
- Defined in:
- app/models/ability.rb
Overview
CanCan permissions
Instance Method Summary collapse
-
#initialize(user) ⇒ Ability
constructor
rubocop:disable MethodLength,AbcSize.
Constructor Details
#initialize(user) ⇒ Ability
rubocop:disable MethodLength,AbcSize
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/models/ability.rb', line 10 def initialize(user) # Define abilities for the passed in user here. # # See the wiki for details:key => "value", # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities can :manage, User, id: user.id can :manage, UserProfile, user_id: user.id role = user.find_profile.role_name machines = [TravelSponsorship, Reimbursement, Shipment] machines.each do |machine| # For all roles, transitions are possible if the state machine allows them # (can_foo?) and if the role is authorized (allow_foo?) machine.transitions.each do |action| can action, machine do |m| # For own machines, the :requester permissions are applied if m.user == user m.send("can_#{action}?") && m.send("allow_#{action}?", :requester) # :requester is not longer a valid role elsif role == 'requester' false # For machines belonging to other users, use the role else m.send("can_#{action}?") && m.send("allow_#{action}?", role) end end end # Comments conds = { machine_type: machine.base_class.to_s } conds[:machine] = { type: machine.to_s } if machine.superclass != ApplicationRecord if machine.allow_private_comments?(role) can %i[read create], Comment, conds elsif machine.allow_public_comments?(role) can %i[read create], Comment, conds.merge(private: false) end requester_conds = conds.deep_dup requester_conds[:machine] = {} unless requester_conds[:machine] requester_conds[:machine][:user_id] = user.id if machine.private_comments_for_requester? can %i[read create], Comment, requester_conds elsif machine.public_comments_for_requester? can %i[read create], Comment, requester_conds.merge(private: false) end end # # Regular users permissions # ------------------------- # # Events can %i[read create], Event can [:participants], Event do |e| user.organizing_events.exists?(e.id) end can :update, Event, &:editable_by_requesters? can :destroy, Event do |e| e.editable_by_requesters? && e.can_be_destroyed? end # Abilities for an event organizer user.organizing_events.each do |e| can :manage, EventEmail, event: { id: e.id } can :read, TravelSponsorship, event_id: e.id end # TravelSponsorships can :create, TravelSponsorship do |r| r.event&.accepting_requests? end can :create, RequestExpense do |e| e.request&.editable? && e.request&.user == user end can :read, TravelSponsorship, user_id: user.id can :update, TravelSponsorship do |r| r.user == user && r.editable? end can :destroy, TravelSponsorship do |r| r.user == user && r.can_be_destroyed? end # Reimbursements can :create, Reimbursement do |r| r.request.user == user && r.request.can_have_reimbursement? end can :read, Reimbursement, user_id: user.id can :update, Reimbursement do |r| r.user == user && r.editable? end # Reimbursement's attachments can :read, ReimbursementAttachment do |a| a.reimbursement.user == user end can %i[create update destroy], ReimbursementAttachment do |a| a.reimbursement.user == user && a.reimbursement.editable? end # Reimbursement's bank account can :read, BankAccount do |a| a.reimbursement.user == user end can %i[create update], BankAccount do |a| a.reimbursement.user == user && a.reimbursement.editable? end # Reimbursement's payments can :read, Payment, reimbursement: { user_id: user.id } # Shipments can :create, Shipment do |s| s.event&.accepting_shipments? end can :read, Shipment, user_id: user.id can :update, Shipment do |s| s.user == user && s.editable? end can :destroy, Shipment do |s| s.user == user && s.can_be_destroyed? end # FIXME: workaround (see below) report_full_access = false # # TSP members permissions # ----------------------- # if role == 'tsp' # User profiles can :read, UserProfile # Budgets can :manage, Budget # Events can %i[update validate participants], Event can :destroy, Event, &:can_be_destroyed? # Email Events can :manage, EventEmail # Event Organizers can :manage, EventOrganizer # TravelSponsorships can :read, TravelSponsorship # Reimbursements can :read, Reimbursement can :read, ReimbursementAttachment can :read, BankAccount can :read, Payment # Expenses Reports can :read, TravelExpenseReport report_full_access = true end # # TSP assistants permissions # -------------------------- # if role == 'assistant' # User profiles can :read, UserProfile # Budgets can :read, Budget # Requests can :read, TravelSponsorship # Reimbursements can :read, Reimbursement can :read, ReimbursementAttachment can :read, BankAccount can :read, Payment # Expenses Reports can :read, TravelExpenseReport report_full_access = true end # # Administratives permissions # ----------------------- # if role == 'administrative' # User profiles can :read, UserProfile # TravelSponsorships can :read, TravelSponsorship # Reimbursements can :read, Reimbursement can :read, ReimbursementAttachment can :read, BankAccount can %i[read create update destroy], Payment end # # Supervisors permissions # -------------------------------------- # if role == 'supervisor' # User profiles can :read, UserProfile # Budgets can :manage, Budget # Events can %i[update validate], Event can :destroy, Event, &:can_be_destroyed? # TravelSponsorships can :read, TravelSponsorship # Can create state adjustments can :adjust_state, TravelSponsorship # Reimbursements can :read, Reimbursement can :read, ReimbursementAttachment can :read, BankAccount can :read, Payment # Can create state adjustments can :adjust_state, Reimbursement # Shipments can :read, Shipment # Or even create state adjustments can :adjust_state, Shipment # Comments can %i[read create], Comment # Expenses Reports can :read, TravelExpenseReport report_full_access = true end # # Material manager permissions # ---------------------------- # if role == 'material' # User profiles can :read, UserProfile # Events can %i[update validate], Event can :destroy, Event, &:can_be_destroyed? # Shipments can :read, Shipment end # # Shipper permissions # ------------------- # if role == 'shipper' cannot :create, TravelSponsorship # Shipments can :read, Shipment end # FIXME: workaround # CanCanCan cannot merge Active Record scope with other conditions return if report_full_access can :read, TravelExpenseReport, TravelExpenseReport.(user) do |e| e.(user) end end |