Module: Subroutine::Auth

Extended by:
ActiveSupport::Concern
Defined in:
lib/subroutine/auth.rb,
lib/subroutine/auth/not_authorized_error.rb,
lib/subroutine/auth/authorization_not_declared_error.rb

Defined Under Namespace

Modules: ClassMethods Classes: AuthorizationNotDeclaredError, NotAuthorizedError

Instance Method Summary collapse

Instance Method Details

#authorize_no_user_requiredObject



156
157
158
# File 'lib/subroutine/auth.rb', line 156

def authorize_no_user_required
  unauthorized! :empty_unauthorized if current_user.present?
end

#authorize_user_not_requiredObject



148
149
150
# File 'lib/subroutine/auth.rb', line 148

def authorize_user_not_required
  true
end

#authorize_user_requiredObject



152
153
154
# File 'lib/subroutine/auth.rb', line 152

def authorize_user_required
  unauthorized! unless current_user.present?
end

#current_userObject



132
133
134
135
# File 'lib/subroutine/auth.rb', line 132

def current_user
  @current_user = user_class_name.constantize.find(@current_user) if ::Integer === @current_user
  @current_user
end

#initialize(*args, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/subroutine/auth.rb', line 100

def initialize(*args, &block)
  raise Subroutine::Auth::AuthorizationNotDeclaredError unless self.class.authorization_declared?

  @skip_auth_checks = false

  inputs = case args.last
  when *::Subroutine::Fields.allowed_input_classes
    args.pop
  else
    {}
  end

  super(inputs, &block)

  user = args.shift

  unless self.class.supported_user_class_names.include?(user.class.name)
    raise ArgumentError, "current_user must be one of the following types {#{self.class.supported_user_class_names.join(",")}} but was #{user.class.name}"
  end

  @current_user = user
end

#skip_auth_checks!Object



123
124
125
126
# File 'lib/subroutine/auth.rb', line 123

def skip_auth_checks!
  @skip_auth_checks = true
  self
end

#skip_auth_checks?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/subroutine/auth.rb', line 128

def skip_auth_checks?
  !!@skip_auth_checks
end

#unauthorized!(reason = nil) ⇒ Object



137
138
139
140
# File 'lib/subroutine/auth.rb', line 137

def unauthorized!(reason = nil)
  reason ||= :unauthorized
  raise ::Subroutine::Auth::NotAuthorizedError, reason
end

#validate_authorization_checksObject



142
143
144
145
146
# File 'lib/subroutine/auth.rb', line 142

def validate_authorization_checks
  authorization_checks.each do |check|
    send(check)
  end
end