Module: Subroutine::Auth

Defined in:
lib/subroutine/auth.rb

Defined Under Namespace

Modules: ClassMethods Classes: AuthorizationNotDeclaredError, NotAuthorizedError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/subroutine/auth.rb', line 23

def self.included(base)
  base.instance_eval do
    extend ::Subroutine::Auth::ClassMethods

    class_attribute :authorization_declared, instance_writer: false
    self.authorization_declared = false

    class_attribute :user_class_name, instance_writer: false
    self.user_class_name = "User"
  end
end

Instance Method Details

#current_userObject



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

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

#initialize(*args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/subroutine/auth.rb', line 101

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

  super(args.extract_options!)
  @skip_auth_checks = false
  @current_user = args.shift

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

#skip_auth_checks!Object



113
114
115
116
# File 'lib/subroutine/auth.rb', line 113

def skip_auth_checks!
  @skip_auth_checks = true
  self
end

#skip_auth_checks?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/subroutine/auth.rb', line 118

def skip_auth_checks?
  !!@skip_auth_checks
end

#unauthorized!(reason = nil) ⇒ Object



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

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