Module: Tiun::Auth

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

Constant Summary collapse

Controller =
Tiun::Auth::Controller

Instance Method Summary collapse

Instance Method Details

#auth_contextObject



60
61
62
# File 'lib/tiun/auth.rb', line 60

def auth_context
   @auth_context ||= { except: supplement_names, locales: locales }
end

#authenticate_userObject



24
25
26
# File 'lib/tiun/auth.rb', line 24

def authenticate_user
   current_user
end

#contextObject



64
65
66
# File 'lib/tiun/auth.rb', line 64

def context
   @context ||= { except: supplement_names, locales: locales }
end

#current_userObject

TODO add automatic with list



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tiun/auth.rb', line 9

def current_user
   @current_user ||=
     #if session["user"] && user = User.where(id: session["user"]["id"]).first
      if session["user"] && user = User.with_user_names(auth_context).with_descriptions(auth_context).with_accounts(auth_context).where(id: session["user"]["id"]).first
         session_data = serialize_collection(user.update_session(session["user"]["refresh_token"]), auth_context)
         session.update("user" => session["user"].merge(session_data))

         user
      end
rescue Tiun::Model::Auth::InvalidTokenError
   session.delete('user')

   nil
end

#get_properties(for_object = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tiun/auth.rb', line 38

def get_properties for_object = nil
   @get_properties ||=
      if k = self.class.instance_variable_get(:@context)&.[](action_name)&.kind
         Tiun.type_attributes_for(k)
      else
         case for_object
         when ActiveRecord::Reflection, ActiveRecord::Associations, ActiveRecord::Scoping, ActiveRecord::Base
            for_object.model
         when Array
            for_object.find { |x| x.respond_to?(:attribute_types) } ||
               for_object.find { |x| x.class.respond_to?(:attribute_types) }.class
         when Hash
            for_object.values.find { |x| x.respond_to?(:attribute_types) } ||
               for_object.values.find { |x| x.class.respond_to?(:attribute_types) }.class
         when NilClass
            model
         else
            for_object
         end.attribute_types.keys
      end
end

#localesObject



68
69
70
# File 'lib/tiun/auth.rb', line 68

def locales
   @locales ||= [I18n.locale]
end

#modelObject



32
33
34
35
36
# File 'lib/tiun/auth.rb', line 32

def model
   @_model ||= model_name.constantize
rescue NameError
   User
end

#model_nameObject



28
29
30
# File 'lib/tiun/auth.rb', line 28

def model_name
   @_model_name ||= self.class.to_s.gsub(/.*::/, "").gsub("Controller", "").singularize
end

#serialize(object, context = self.context) ⇒ Object



80
81
82
# File 'lib/tiun/auth.rb', line 80

def serialize object, context = self.context
   object.as_json(context.merge(only: get_properties(object)))
end

#serialize_collection(collection, context = self.context) ⇒ Object



76
77
78
# File 'lib/tiun/auth.rb', line 76

def serialize_collection collection, context = self.context
   collection.as_json(context.merge(only: get_properties(collection)))
end

#supplement_namesObject



72
73
74
# File 'lib/tiun/auth.rb', line 72

def supplement_names
   %i(created_at updated_at tsv)
end