Class: Caboose::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/user.rb

Constant Summary collapse

ADMIN_USER_ID =
1
LOGGED_OUT_USER_ID =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.logged_out_user(site_id) ⇒ Object


27
28
29
30
# File 'app/models/caboose/user.rb', line 27

def self.logged_out_user(site_id)
  return self.where(:site_id => site_id, :username => 'elo').first
  #return self.where(:id => self::LOGGED_OUT_USER_ID).first
end

.logged_out_user_id(site_id) ⇒ Object


32
33
34
35
# File 'app/models/caboose/user.rb', line 32

def self.logged_out_user_id(site_id)
  return self.where(:site_id => site_id, :username => 'elo').limit(1).pluck(:id)[0]
  #return self::LOGGED_OUT_USER_ID
end

.user_for_reset_id(reset_id) ⇒ Object


135
136
137
138
139
140
141
142
# File 'app/models/caboose/user.rb', line 135

def self.user_for_reset_id(reset_id)          
  return nil if reset_id.nil?          
  d = DateTime.now - 3.days
  if self.where("password_reset_id = ? and password_reset_sent > ?", reset_id, d).exists?
    return self.where("password_reset_id = ? and password_reset_sent > ?", reset_id, d).first
  end
  return nil
end

.validate_token(token) ⇒ Object

def is_allowed(resource, action)

if self.username == 'elo'
  elo = Caboose::Role.logged_out_role(self.site_id)
  elo_is_allowed = elo.is_allowed(resource, action)    
  return true if elo_is_allowed
  return false if !elo_is_allowed && self.is_logged_out_user?     
  eli = Caboose::Role.logged_in_role(self.site_id)
  return true if self.id != elo.id && eli.is_allowed(resource, action)
else
  for role in roles
    if role.is_allowed(resource, action)
      return true
    end
  end
end
return false

end


87
88
89
90
# File 'app/models/caboose/user.rb', line 87

def self.validate_token(token)
  user = self.where('token' => token).first
  return user 
end

Instance Method Details

#add_to_role(role_id) ⇒ Object


98
99
100
101
102
103
104
105
106
107
# File 'app/models/caboose/user.rb', line 98

def add_to_role(role_id)
  r = Caboose::Role.find(role_id)
  return false if r.nil?
  
  if (!is_member?(r.id))
    roles.push r
    save
  end
  return true
end

#add_to_role_with_name(role_name) ⇒ Object


92
93
94
95
96
# File 'app/models/caboose/user.rb', line 92

def add_to_role_with_name(role_name)
  r = Caboose::Role.where(:name => role_name).first
  return false if r.nil?
  return add_to_role(r.id)
end

#card_is_expired?Boolean

Returns:

  • (Boolean)

163
164
165
166
167
# File 'app/models/caboose/user.rb', line 163

def card_is_expired?
  return true if self.stripe_customer_id.nil?
  d = Date.new(self.card_exp_year, self.card_exp_month, 1)
  return d <= Date.today
end

#create_variant_limitsObject


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/caboose/user.rb', line 109

def create_variant_limits
  elo = Caboose::User.where(:site_id => self.site_id, :username => 'elo').first if !self.site_id.blank?
  variant_limits = Caboose::VariantLimit.where(:user_id => elo.id).all if elo
  if variant_limits && variant_limits.count > 0
    variant_limits.each do |vl|
      new_vl = Caboose::VariantLimit.where(:user_id => self.id, :variant_id => vl.variant_id).first
      if new_vl.nil?
        new_vl = Caboose::VariantLimit.create(:user_id => self.id, :variant_id => vl.variant_id)
        new_vl.min_quantity_value = vl.min_quantity_value
        new_vl.min_quantity_func = vl.min_quantity_func
        new_vl.max_quantity_value = vl.max_quantity_value
        new_vl.max_quantity_func = vl.max_quantity_func
        new_vl.current_value = 0
        new_vl.save
      end
    end
  end
end

#forem_emailObject


49
50
51
# File 'app/models/caboose/user.rb', line 49

def forem_email
  self.email
end

#forem_nameObject


45
46
47
# File 'app/models/caboose/user.rb', line 45

def forem_name
  self.username
end

#is_allowed(resource, action) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/caboose/user.rb', line 53

def is_allowed(resource, action)
  return true if self.is_super_admin?       
  elo = Caboose::Role.logged_out_role(self.site_id)
  elo_is_allowed = elo.is_allowed(resource, action)    
  return true if elo_is_allowed
  return false if !elo_is_allowed && self.is_logged_out_user?     
  eli = Caboose::Role.logged_in_role(self.site_id)
  return true if self.id != elo.id && eli.is_allowed(resource, action)
  for role in roles
    if role.is_allowed(resource, action)
      return true
    end
  end
  return false
end

#is_logged_out_user?Boolean

Returns:

  • (Boolean)

37
38
39
# File 'app/models/caboose/user.rb', line 37

def is_logged_out_user?
  return self.id == Caboose::User.logged_out_user_id(self.site_id)    
end

#is_member?(role_id) ⇒ Boolean

Returns:

  • (Boolean)

128
129
130
131
132
133
# File 'app/models/caboose/user.rb', line 128

def is_member?(role_id)
  roles.each do |r|
    return true if (r.id == role_id)
  end
  return false
end

#is_super_admin?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'app/models/caboose/user.rb', line 41

def is_super_admin?
  return self.username == 'superadmin'    
end

#toggle_roles(role_id, value) ⇒ Object


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/caboose/user.rb', line 144

def toggle_roles(role_id, value)            
  if value.to_i > 0 # Add to role                   
    if role_id == 'all'
      Caboose::RoleMembership.where(:user_id => self.id).destroy_all      
      Caboose::Role.where(:site_id => self.site_id).reorder(:name).all.each{ |r| Caboose::RoleMembership.create(:user_id => self.id, :role_id => r.id) }                          
    else
      if !Caboose::RoleMembership.where(:user_id => self.id, :role_id => role_id.to_i).exists?
        Caboose::RoleMembership.create( :user_id => self.id, :role_id => role_id.to_i)
      end      
    end
  else # Remove from role    
    if role_id == 'all'
      Caboose::RoleMembership.where(:user_id => self.id).destroy_all                          
    else
      Caboose::RoleMembership.where(:user_id => self.id, :role_id => role_id.to_i).destroy_all      
    end
  end
end