Module: NexusCqrs::Auth::Ownable

Extended by:
ActiveSupport::Concern
Defined in:
lib/nexus_cqrs/auth/ownable.rb

Overview

Concern used to integrate models to the permissions system. Including this module to a model will assume the model can be “owned” by a user. When the model is created, permissions will automatically be assigned to the user and permissions can be validated and “repaired” retroactively.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_permissionsObject



29
30
31
32
33
34
35
36
37
# File 'lib/nexus_cqrs/auth/ownable.rb', line 29

def assign_permissions
  # As we are doing this on before_create, we must "build" this association, as opposed to creating it. This
  # ensures validation is passed before saving this parent Collection.
  granted_permissions.each do |p|
    unless permissions.where(permission: p, entity_id: id, user_id: owner_id).exists?
      permissions.build(user_id: owner_id, permission: p)
    end
  end
end

#owner_idObject



39
40
41
# File 'lib/nexus_cqrs/auth/ownable.rb', line 39

def owner_id
  send(owner_column)
end

#permissionsObject



43
44
45
46
47
48
49
50
# File 'lib/nexus_cqrs/auth/ownable.rb', line 43

def permissions
  if respond_to?(permissions_relation)
    return send(permissions_relation)
  end

  raise OwnableRelationshipNotSet, "Permissions relation not set.
  Set it on your model with `self.permissions_relation = :xxx_permissions`, and ensure relationship has been created"
end