Class: Maestrano::Connector::Rails::ApplicationPolicy Abstract

Inherits:
Object
  • Object
show all
Defined in:
app/policies/maestrano/connector/rails/application_policy.rb

Overview

This class is abstract.

Abstract base class for all policies

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record) ⇒ ApplicationPolicy

Returns a new instance of BasePolicy

Parameters:

  • user (User)

    the current user

  • record (Object)

    some kind of model object, whose authorization you want to check

Raises:

  • (Pundit::NotAuthorizedError)


16
17
18
19
20
21
22
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 16

def initialize(user, record)
  # Closed system: must be logged in to do anything
  raise Pundit::NotAuthorizedError, 'must be logged in' unless user

  @user = user
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns some kind of model object, whose authorization you want to check.

Returns:

  • (Object)

    some kind of model object, whose authorization you want to check



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
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 9

class Maestrano::Connector::Rails::ApplicationPolicy
  attr_reader :user, :record

  # Returns a new instance of {BasePolicy}
  # @param [User] user the current user
  # @param [Object] record some kind of model object, whose authorization you want to check
  # @return [ApplicationPolicy]
  def initialize(user, record)
    # Closed system: must be logged in to do anything
    raise Pundit::NotAuthorizedError, 'must be logged in' unless user

    @user = user
    @record = record
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    create?
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope_to_tenant
    end

    def scope_to_tenant
      scope.where(tenant: user)
    end
  end
end

#userUser (readonly)

Returns the current user.

Returns:

  • (User)

    the current user



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
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 9

class Maestrano::Connector::Rails::ApplicationPolicy
  attr_reader :user, :record

  # Returns a new instance of {BasePolicy}
  # @param [User] user the current user
  # @param [Object] record some kind of model object, whose authorization you want to check
  # @return [ApplicationPolicy]
  def initialize(user, record)
    # Closed system: must be logged in to do anything
    raise Pundit::NotAuthorizedError, 'must be logged in' unless user

    @user = user
    @record = record
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    create?
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope_to_tenant
    end

    def scope_to_tenant
      scope.where(tenant: user)
    end
  end
end

Instance Method Details

#create?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 24

def create?
  false
end

#destroy?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 40

def destroy?
  false
end

#edit?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 36

def edit?
  update?
end

#new?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 28

def new?
  create?
end

#scopeObject



44
45
46
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 44

def scope
  Pundit.policy_scope!(user, record.class)
end

#update?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/policies/maestrano/connector/rails/application_policy.rb', line 32

def update?
  create?
end