Module: Milia::Control

Defined in:
lib/milia/control.rb

Defined Under Namespace

Modules: ClassMethods Classes: InvalidTenantAccess, MaxTenantExceeded

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

#############################################################################



9
10
11
# File 'lib/milia/control.rb', line 9

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#__milia_change_tenant!(tid) ⇒ Object





25
26
27
28
29
30
31
# File 'lib/milia/control.rb', line 25

def __milia_change_tenant!( tid )
  old_id = ( Thread.current[:tenant_id].nil? ? '%' : Thread.current[:tenant_id] )
  new_id = ( tid.nil? ? '%' : tid.to_s )
  Thread.current[:tenant_id] = tid
  session[:tenant_id] = tid  # remember it going forward
  logger.debug("MILIA >>>>> [change tenant] new: #{new_id}\told: #{old_id}") unless logger.nil?
end

#__milia_reset_tenant!Object





35
36
37
38
# File 'lib/milia/control.rb', line 35

def __milia_reset_tenant!
  __milia_change_tenant!( nil )
  logger.debug("MILIA >>>>> [reset tenant] ") unless logger.nil?
end

#after_sign_in_path_for(resource_or_scope) ⇒ Object





202
203
204
# File 'lib/milia/control.rb', line 202

def (resource_or_scope)
  welcome_path
end

#after_sign_out_path_for(resource_or_scope) ⇒ Object

Overwriting the sign_out redirect path method



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/milia/control.rb', line 188

def after_sign_out_path_for(resource_or_scope)

  if ::Milia.signout_to_root
    root_path        # return to index page
  else
      # or return to sign-in page
   scope = Devise::Mapping.find_scope!(resource_or_scope)
   send(:"new_#{scope}_session_path")
  end

end

#after_sign_up_path_for(resource_or_scope) ⇒ Object





209
210
211
# File 'lib/milia/control.rb', line 209

def (resource_or_scope)
  root_path
end

#authenticate_tenant!Object


authenticate_tenant! – authorization & tenant setup – authenticates user – sets current tenant


Raises:

  • (SecurityError)


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

def authenticate_tenant!
  unless current_user.present? || authenticate_user!(force: true)
    email = ( params.nil? || params[:user].nil?  ?  "<email missing>"  : params[:user][:email] )
    flash[:error] = "cannot sign in as #{email}; check email/password"
    logger.info("MILIA >>>>> [failed auth user] ") unless logger.nil?
  end

  trace_tenanting( "authenticate_tenant!" )

  # user_signed_in? == true also means current_user returns valid user
  raise SecurityError,"*** invalid user_signed_in  ***" unless user_signed_in?

  set_current_tenant   # relies on current_user being non-nil

    # successful tenant authentication; do any callback
  if self.respond_to?( :callback_authenticate_tenant, true )
    logger.debug("MILIA >>>>> [auth_tenant callback]")
    self.send( :callback_authenticate_tenant )
  end

  true  # allows before filter chain to continue
end

#initiate_tenant(tenant) ⇒ Object


initiate_tenant – initiates first-time tenant; establishes thread assumes not in a session yet (since here only upon new account sign-up) ONLY for brand-new tenants upon User account sign up arg

tenant -- tenant obj of the new tenant



89
90
91
# File 'lib/milia/control.rb', line 89

def initiate_tenant( tenant )
    __milia_change_tenant!( tenant.id )
end

#invalid_tenantObject


invalid_tenant – using wrong or bad data




143
144
145
146
# File 'lib/milia/control.rb', line 143

def invalid_tenant
  flash[:error] = "Wrong tenant access"
  redirect_back
end

#klass_option_obj(klass, option_obj) ⇒ Object




165
166
167
168
169
# File 'lib/milia/control.rb', line 165

def klass_option_obj(klass, option_obj)
  return option_obj if option_obj.instance_of?(klass)
  option_obj ||= {}  # if nil, makes it empty hash
  return klass.send( :new, option_obj )
end

#max_tenantsObject





126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/milia/control.rb', line 126

def max_tenants
  logger.info(
    "MILIA >>>>> [max tenant signups] #{Time.now.to_s(:db)} - User: '#{params[:user].try(:email)}', Tenant: '#{params[:tenant].try(:name)}'"
  ) unless logger.nil?

  flash[:error] = "Sorry: new accounts not permitted at this time"

    # if using Airbrake & airbrake gem
  if ::Milia.use_airbrake
    notify_airbrake( $! )  # have airbrake report this -- requires airbrake gem
  end
  redirect_back
end

#prep_signup_view(tenant = nil, user = nil, coupon = {coupon:''}) ⇒ Object




179
180
181
182
183
# File 'lib/milia/control.rb', line 179

def (tenant=nil, user=nil, coupon={coupon:''})
   @user   = klass_option_obj( User, user )
   @tenant = klass_option_obj( Tenant, tenant )
   @coupon = coupon #  if ::Milia.use_coupon
end

#redirect_backObject


redirect_back – bounce client back to referring page




151
152
153
# File 'lib/milia/control.rb', line 151

def redirect_back
  super(fallback_location: root_path)
end

#set_current_tenant(tenant_id = nil) ⇒ Object


set_current_tenant – sets the tenant id for the current invocation (thread) args

tenant_id -- integer id of the tenant; nil if get from current user

EXCEPTIONS – InvalidTenantAccess




58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/milia/control.rb', line 58

def set_current_tenant( tenant_id = nil )

  if user_signed_in?

    @_my_tenants ||= current_user.tenants  # gets all possible tenants for user

    tenant_id ||= session[:tenant_id]   # use session tenant_id ?

    if tenant_id.nil?  # no arg; find automatically based on user
      tenant_id = @_my_tenants.first.id  # just pick the first one
    else   # validate the specified tenant_id before setup
      raise InvalidTenantAccess unless @_my_tenants.any?{|tu| tu.id == tenant_id}
    end

  else   # user not signed in yet...
    tenant_id = nil   # an impossible tenant_id
  end

  __milia_change_tenant!( tenant_id )
  trace_tenanting( "set_current_tenant" )

  true    # before filter ok to proceed
end

#trace_tenanting(fm_msg) ⇒ Object





42
43
44
45
46
47
48
49
50
# File 'lib/milia/control.rb', line 42

def trace_tenanting( fm_msg )
  if ::Milia.trace_on
    tid = ( session[:tenant_id].nil? ? "%/#{Thread.current[:tenant_id]}" : session[:tenant_id].to_s )
    uid = ( current_user.nil?  ?  "%/#{session[:user_id]}"  : "#{current_user.id}")
    logger.debug(
       "MILIA >>>>> [#{fm_msg}] stid: #{tid}\tuid: #{uid}\tus-in: #{user_signed_in?}"
    ) unless logger.nil?
  end # trace check
end