mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-09 03:20:21 +00:00
I removed the caching of `managed_enterprises` in Permissions because it's just a scope and calling it again is very cheap. And that makes the method a lot easier to read now that we have a conditional here. Accessing the managed enterprises via the user instead of a separate scope on the Enterprise model also reduce the SQL queries. We may want to use this method in more places. I prefer to keep the admin-conditional in a permissions class instead of in the model.
37 lines
581 B
Ruby
37 lines
581 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Authorised user or client using the API
|
|
class ApiUser
|
|
CLIENT_MAP = {
|
|
"https://waterlooregionfood.ca/portal/profile" => "cqcm-dev",
|
|
}.freeze
|
|
|
|
def self.from_client_id(client_id)
|
|
id = CLIENT_MAP[client_id]
|
|
|
|
new(id) if id
|
|
end
|
|
|
|
attr_reader :id
|
|
|
|
def initialize(id)
|
|
@id = id
|
|
end
|
|
|
|
def admin?
|
|
false
|
|
end
|
|
|
|
def customers
|
|
Customer.none
|
|
end
|
|
|
|
def enterprises
|
|
Enterprise.where(dfc_permissions: permissions("ReadEnterprise"))
|
|
end
|
|
|
|
def permissions(scope)
|
|
DfcPermission.where(grantee: id, scope:)
|
|
end
|
|
end
|