Created
June 24, 2014 16:42
-
-
Save owen2345/b963bb9faaa265cd1e35 to your computer and use it in GitHub Desktop.
Rails model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::User < ActiveRecord::Base | |
has_one :profile , :class_name => "Admin::Profile", :foreign_key => "user_id", dependent: :destroy | |
has_many :assigned_user, :class_name => "Admin::AssignedUser", dependent: :destroy | |
has_many :assigned_site, -> {where(:is_disabled => 0)}, :class_name => "Admin::AssignedUser", dependent: :destroy | |
has_many :sites, :class_name => "Admin::Site", :through => :assigned_site | |
has_many :permission, :class_name => "Admin::Permission", :dependent => :destroy | |
has_many :admin_group, :class_name => "Admin::AdminGroup", :through => :assigned_user | |
has_many :visitor_group, :class_name => "Admin::VisitorGroup", :through => :assigned_user | |
has_many :user_page_admin_permission2, :class_name => "Admin::Page", :through => :permission | |
has_many :user_page_visitor_permission2, :class_name => "Admin::Page", :through => :permission | |
# categories assigned to this user direct by username | |
has_many :admin_category, -> {where("permissions.typee = 'admin'")}, :class_name => "Admin::Category", :through => :permission, :source => :category | |
# categories assigned to this user direct by username | |
has_many :visit_category, -> {where("permissions.typee = 'visitor'")}, :class_name => "Admin::Category", :through => :permission, :source => :category | |
has_many :categories, :class_name => "Admin::Category", :through => :permission, :source => :category # get admin and visitor categories | |
has_many :page_rating, :class_name => "Admin::PageRating", :dependent => :destroy | |
has_many :page_follows, -> { where(:typee => 2) }, :class_name => "Admin::PageRating" | |
has_many :page_following, :class_name => "Admin::Page", :through => :page_follows, :source => :page | |
has_many :page_visit, :class_name => "Admin::PageVisit", :dependent => :destroy | |
has_many :category_visit, :class_name => "Admin::CategoryVisit", :dependent => :destroy | |
has_many :page_comment, :class_name => "Admin::PageComment", :dependent => :destroy | |
has_many :page_letter, :class_name => "Admin::PageLetter", :dependent => :destroy | |
has_many :cache_role, :class_name => "Admin::CacheRole", :dependent => :destroy | |
has_many :category_follower, :class_name => "Admin::CategoryFollower", :dependent => :destroy | |
has_many :admin_roles, :class_name => "Admin::AssignedAction", through: :admin_group, source: :assigned_action | |
has_many :sessions, :class_name => "Admin::UserSession", :dependent => :destroy | |
has_many :reports, :class_name => "Admin::Report" ,:foreign_key => "user_id" , :dependent => :destroy | |
has_many :owner_pages, -> { where "pages.typee" => "page" }, :class_name => "Admin::Page", :foreign_key => "owner_id" | |
has_many :owner_categories, :class_name => "Admin::Category", :foreign_key => "owner_id" | |
has_many :comments_my_pages, :class_name => "Admin::PageComment", :through => :owner_pages, :source => :page_comment | |
#auth social | |
has_many :authorizations, :class_name => "Admin::Authorization" ,:foreign_key => "user_id" , :dependent => :destroy | |
has_many :messages_users, :class_name => "Plugins::Messages::MessagesUsers" | |
has_many :messages, :class_name => "Plugins::Messages::Message", through: :messages_users | |
has_many :responses, :class_name => "Plugins::Messages::Response", :foreign_key => "user_id" | |
has_many :to_responses, :class_name => "Plugins::Messages::Response" ,:foreign_key => "to_user_id" | |
has_many :tickets, :class_name => "Plugins::TicketsManager::Ticket", :dependent => :destroy | |
has_many :bookmark, class_name: "Plugins::Bookmark", :dependent => :destroy | |
# PLUGINS | |
has_many :plugins_tab, :class_name => "Plugins::TabsManager::Tab" | |
has_many :plugins_accordion, :class_name => "Plugins::AccordionsManager::Accordion" | |
has_many :plugins_gallery, :class_name => "Plugins::PhotoGallery::Album" | |
has_many :email_visited_notification, :class_name => "Plugins::EmailVisitedNotification", :dependent => :destroy | |
scope :disabled, -> { where(status: 1) } | |
scope :enabled, -> { where(status: 0) } | |
scope :super_admin, -> { where(role: 5) } | |
scope :last_login, -> { order('last_login DESC') } | |
STATUS = {0 => 'Habilitado', 1=>'Deshabilitado'} #valid | |
attr_accessor :password, :password_confirmation, :tmp_pages, :auth_twitter | |
attr_accessible :firstname, :lastname, :email, :phone, :username, :password, :password_confirmation, :role, :avatar, :remote_avatar_url, :status, :path_avatar, :last_login, :date_lower, :profile_attributes, :typee | |
#:typee => authentification method (normal, ad) | |
accepts_nested_attributes_for :profile | |
validates :username, :presence => true, uniqueness: { case_sensitive: false } | |
validates :password, :presence => true, :confirmation => true, :length => { :within => 4..40 }, :on => :create if :auth_twitter.nil? | |
#mount_uploader :avatar, ImageUploader | |
before_save :encrypt_password #callback for create and update | |
#before_update :encrypt_password #callback | |
after_create :auto_assign_to_sites | |
before_save :save_date_status | |
before_destroy :re_assign_owner | |
#before_update :save_date_status | |
##################################### PERM FUNCTIONS ########################################### | |
#check if user was assigned the key_role in site (for admin groups) | |
def assigned_role?(key_role, site) | |
cache = instance_variable_get("@user#{self.id}_perm_#{key_role}_#{site.id}") | |
return cache unless cache.nil? | |
res = false | |
if self.isDisabledFor?(site) | |
res = false | |
elsif self.isSuperAdmin || self.admin_group.where(:site_id => site).joins(:assigned_action).where("assigned_actions.key = '#{key_role}'").present? | |
res = true | |
end | |
instance_variable_set("@user#{self.id}_perm_#{key_role}_#{site.id}", res) | |
res | |
end | |
#check if user was assigned the key_role in site (for visit groups) | |
def assigned_visit_role?(key_role, site) | |
cache = instance_variable_get("@user#{self.id}_visit_perm_#{key_role}_#{site.id}") | |
return cache unless cache.nil? | |
res = false | |
if self.isDisabledFor?(site) | |
res = false | |
elsif self.isSuperAdmin || self.visitor_group.where(:site_id => site).joins(:assigned_action).where("assigned_actions.key = '#{key_role}'").present? | |
res = true | |
end | |
instance_variable_set("@user#{self.id}_visit_perm_#{key_role}_#{site.id}", res) | |
res | |
end | |
# return all groups assigned for type | |
def roles(site, type = "visit") | |
return self.visitor_group.where(:site_id => site.id) if type == "visit" | |
return self.admin_group.where(:site_id => site.id) if type == "admin" | |
end | |
def isSuperAdmin | |
self.role.to_i == 5 | |
end | |
# check user is advanced admin for any post type | |
def is_adv_admin?(site) | |
isSuperAdmin || get_assigned_post_types(site, type = "adv").present? || self.admin_category.where("categories.post_type_id in (#{site.post_type.pluck("post_types.id").join(",")})").present? | |
end | |
# check if current user has assigned 1+ post types as adv or basic | |
def is_basic_admin?(site) | |
is_adv_admin?(site) || get_assigned_post_types(site, type = "basic").present? | |
end | |
def isAdministrator_for?(site) | |
if self.isSuperAdmin | |
return true | |
end | |
self.get_admin_sites.include?(site) | |
end | |
##################################### BASIC FUNCTIONS ########################################### | |
def get_enabled | |
Admin::User.where(:status => 1 ) | |
end | |
def fullname | |
(self.profile.nil?) ? self.username : self.profile.fullname.titleize | |
end | |
def fullname2 | |
(self.profile.nil?) ? self.username : "#{self.profile.name} #{self.profile.lastname}".titleize | |
end | |
def ip | |
user_sessions = self.sessions.reorder("user_sessions.created_at DESC").first() | |
(user_sessions.nil?)? "" : user_sessions.ip | |
end | |
def getSocialAuth(provider) | |
self.authorizations.where("provider = '#{provider}'").first() | |
end | |
def created | |
self.created_at.strftime('%d/%m/%Y %H:%M:%S') | |
end | |
def updated | |
self.updated_at.strftime('%d/%m/%Y %H:%M:%S') | |
end | |
def lastlogin | |
self.last_login.strftime('%d/%m/%Y %H:%M') if self.last_login.present? | |
end | |
#check if user was disabled from the all system | |
def isDisabled? | |
self.status == 1 | |
end | |
#check if user was disabled on site | |
def isDisabledFor?(site) | |
return true if self.isDisabled? | |
cache = instance_variable_get("@o#{self.id}_isDisabledFor_site_#{site.id}") | |
return cache unless cache.nil? | |
res = self.assigned_user.where(:site_id => site.id).first.is_disabled == 1 | |
instance_variable_set("@o#{self.id}_isDisabledFor_site_#{site.id}", res) | |
res | |
end | |
##################################### START ADMIN FUNCTIONS ########################################### | |
#assign User to group from site | |
def assignToGroup(group, site) | |
as_u = get_assignedToSite(site) | |
group.assigned_group.create({:assigned_user_id => as_u.id}) | |
end | |
# get all post types assigned to this user as type: adv or basic | |
def get_assigned_post_types(site, type = "adv") | |
res = [] | |
site.post_type.each do |ptype| # post types assigned to this user as adv | |
res << ptype.id if self.assigned_role?("post_type_#{ptype.id}_#{type}", site) | |
end | |
res | |
end | |
#get site where this user has role as administrator or is assigned as administrator for any page | |
def get_admin_sites | |
res = [] | |
return sites = Admin::Site.all | |
if self.isSuperAdmin | |
res = sites | |
else | |
sites.each do |site| | |
#this user assigned to any admin group | |
if self.admin_group.where(:site_id => site.id).count > 0 | |
res.push(site) | |
else | |
post_type_where = "post_type_id in (#{site.post_type.ids.join(",")})" | |
#check if user is assigned as admin in any page or this user is owner of any page | |
if self.user_page_admin_permission2.where(post_type_where).count > 0 || site.page.where(:owner_id => self.id).count > 0 | |
res.push(site) | |
else | |
#check if user was assigned as administrator for any category | |
res.push(site) if self.admin_category.where(post_type_where).count > 0 | |
end | |
end | |
end | |
end | |
res | |
end | |
# get admin categories for this user in post_type or site object without dependencies or sub categories | |
# post_type = post_type object or site | |
def __get_admin_categories(post_type) | |
res = Admin::Category.where("1=0") | |
if post_type.class.name == "Admin::Site" | |
site = post_type | |
cache = instance_variable_get("@cache_admin_cat#{self.id}_site_#{site.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(site) | |
res = res | |
elsif self.isSuperAdmin | |
res = site.all_categories | |
else | |
res = site.all_categories.where("categories.id in (#{get_cache(site.id, "admin").category_ids.to_s.split(",").fix_in_sql.join(",")})") | |
end | |
instance_variable_set("@cache_admin_cat#{self.id}_site_#{post_type.id}", res) | |
else # post types | |
site = post_type.site | |
cache = instance_variable_get("@cache_admin_cat#{self.id}_post_type_#{post_type.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(site) | |
res = res | |
elsif self.isSuperAdmin || self.assigned_role?("post_type_#{post_type.id}_adv", site) | |
res = post_type.all_categories | |
else | |
res = post_type.all_categories.where("categories.id in (#{get_cache(site.id, "admin").category_ids.to_s.split(",").fix_in_sql.join(",")})") | |
end | |
instance_variable_set("@cache_admin_cat#{self.id}_post_type_#{post_type.id}", res) | |
end | |
res | |
end | |
# get all pages that this user can admin for site or post type | |
def __get_admin_pages(site_or_post_type) | |
res = Admin::Page.where("1=0") | |
if site_or_post_type.class.name == "Admin::Site" | |
site = site_or_post_type | |
cache = instance_variable_get("@cache_admin_pages_#{self.id}_site_#{site.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(site) | |
res = res | |
elsif self.isSuperAdmin | |
res = site.page_all | |
else | |
res = site.page_all.where("pages.id in (#{get_cache(site.id, "admin").page_ids.to_s.split(",").fix_in_sql.join(",")})") | |
end | |
instance_variable_set("@cache_admin_pages_#{self.id}_site_#{site.id}", res) | |
else # case post type | |
site = site_or_post_type.site | |
post_type = site_or_post_type | |
cache = instance_variable_get("@cache_admin_pages_#{self.id}_post_type_#{post_type.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(site) | |
res = res | |
elsif self.isSuperAdmin || self.assigned_role?("post_type_#{post_type.id}_adv", site) | |
res = post_type.pages_all | |
else | |
res = post_type.pages_all.where("pages.id in (#{get_cache(site.id, "admin").page_ids.to_s.split(",").fix_in_sql.join(",")})") | |
end | |
instance_variable_set("@cache_admin_pages_#{self.id}_post_type_#{post_type.id}", res) | |
end | |
res | |
end | |
#*************** FUNCTIONS USED FOR CACHE **********************## | |
# get admin categories for this user in post_type or site object without dependencies or sub categories | |
# post_type = post_type object or site | |
def get_admin_categories(post_type) | |
res = Admin::Category.where("1=0") | |
if post_type.class.name == "Admin::Site" | |
site = post_type | |
cache = instance_variable_get("@cache_admin_cat#{self.id}_site_#{site.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(site) | |
res = res | |
elsif self.isSuperAdmin | |
res = site.all_categories | |
else | |
p = get_assigned_post_types(site, "adv") | |
cat_ids = self.admin_category.where("categories.post_type_id in (#{site.post_type.pluck("post_types.id").join(",")})").pluck("categories.id", "full_children").join_pluck.to_i.uniq.clean_empty.fix_in_sql.join(",") | |
return site.all_categories.where("categories.post_type_id in (#{p.fix_in_sql.join(",")}) or categories.id in (#{cat_ids}) ") | |
end | |
instance_variable_set("@cache_admin_cat#{self.id}_site_#{post_type.id}", res) | |
else # post types | |
cache = instance_variable_get("@cache_admin_cat#{self.id}_post_type_#{post_type.id}") | |
return cache unless cache.nil? | |
if self.isDisabledFor?(post_type.site) | |
res = res | |
elsif self.isSuperAdmin || self.assigned_role?("post_type_#{post_type.id}_adv", post_type.site) | |
res = post_type.all_categories | |
else | |
direct_cats = self.admin_category.where("categories.post_type_id = #{post_type.id}").pluck("categories.id", "full_children").join_pluck.to_i.uniq.clean_empty | |
res = post_type.all_categories.where("categories.id in (#{direct_cats.fix_in_sql.join(",")})") if direct_cats.present? | |
end | |
instance_variable_set("@cache_admin_cat#{self.id}_post_type_#{post_type.id}", res) | |
end | |
res | |
end | |
# get all pages that this user can admin for site or post type | |
def get_admin_pages(site_or_post_type) | |
res = Admin::Page.where("1=0") | |
if site_or_post_type.class.name == "Admin::Site" | |
site = site_or_post_type | |
return Admin::Page.where("1=0") if self.isDisabledFor?(site) | |
return site.page_all if self.isSuperAdmin | |
p = get_assigned_post_types(site, "adv") | |
### | |
cat_ids = self.admin_category.where("categories.post_type_id in (#{site.post_type.pluck("post_types.id").join(",")})").pluck("categories.id", "full_children").join_pluck.to_i.clean_empty.fix_in_sql.join(",") | |
category_pages_id = Admin::Page.joins(:assigned_page).where("assigned_pages.category_id in (#{cat_ids})").pluck("pages.id").clean_empty.fix_in_sql.join(",") | |
return site.page_all.where("pages.post_type_id in (#{p.clean_empty.fix_in_sql.join(",")}) or pages.id in (#{category_pages_id}) or pages.owner_id = #{self.id}") | |
else # case post type | |
post_type = site_or_post_type | |
return Admin::Page.where("1=0") if self.isDisabledFor?(post_type.site) | |
return post_type.pages_all if self.isSuperAdmin || self.assigned_role?("post_type_#{post_type.id}_adv", post_type.site) | |
direct_cats = self.admin_category.where("categories.post_type_id = #{post_type.id}").pluck("categories.id", "full_children").join_pluck.to_i.uniq | |
return post_type.pages_all.joins(:assigned_page).where("assigned_pages.category_id in (#{direct_cats.fix_in_sql.join(",")}) or pages.owner_id = #{self.id}") | |
end | |
res | |
end | |
#*************** END FUNCTIONS USED FOR CACHE **********************# | |
################VISIT################ | |
# include categories and sub categories | |
def get_visit_category_ids(site) | |
tmp = get_cache(site.id, "visit").category_ids.split(",") + site.get_cache_public.category_ids.split(",") | |
tmp.clean_empty.to_i | |
end | |
# get visit array category_ids for this user | |
def get_visit_categories(site) | |
Admin::Category.where("categories.id in (#{get_visit_category_ids(site).fix_in_sql.join(",")})").visible_front | |
end | |
# get admin array category_ids for this user | |
def get_visit_pages_ids(site, public_pages = true) | |
tmp = Array.new | |
tmp += get_cache(site.id, "visit").page_ids.split(",") unless get_cache(site.id, "visit").nil? | |
tmp += site.get_cache_public.page_ids.split(",") if public_pages | |
tmp.clean_empty.to_i | |
end | |
# get visit array section_ids for this user | |
def get_visit_section_ids(site) | |
tmp = get_cache(site.id, "visit").section_ids.split(",") + site.get_cache_public.section_ids.split(",") | |
tmp.clean_empty.to_i | |
end | |
# get all pages that this user can view on frontend | |
def get_visit_pages(site_or_post_type, public_pages = true) | |
if site_or_post_type.class.name == "Admin::Site" # case site | |
site = site_or_post_type | |
Admin::Page.where("pages.id in (#{get_visit_pages_ids(site, public_pages).fix_in_sql.join(",")}) or pages.owner_id = '#{self.id}'").visible_front | |
else #case post type | |
site = site_or_post_type.site | |
site_or_post_type.pages.where("pages.id in (#{get_visit_pages_ids(site, public_pages).fix_in_sql.join(",")}) or pages.owner_id = '#{self.id}'").visible_front | |
end | |
end | |
def get_visit_sections(site) | |
Admin::Page.where("pages.id in (#{get_visit_section_ids(site).fix_in_sql.join(",")}) or pages.owner_id = '#{self.id}'") | |
end | |
#------------------------------ FOR REPORTS -----------------------------------# | |
def page_search | |
self.reports.where(:typee => 'search') | |
#Admin::Report.where('') | |
end | |
def page_follows | |
self.page_rating.follows | |
#Admin::Report.where('') | |
end | |
##################################### START AUTHENTIFICATION ########################################### | |
def self.authenticate_with_salt(id, cookie_salt) | |
user = find_by_id(id) | |
(user && user.salt == cookie_salt) ? user : nil | |
end | |
def has_password?(submitted_password) | |
encrypted_password == encrypt(submitted_password) | |
end | |
def self.authenticate(username, submitted_password) | |
user = find_by_username(username) | |
return nil if user.nil? | |
return user if user.has_password?(submitted_password) | |
end | |
def getStatusText | |
@a = "#{self.class.name}::STATUS" | |
if self.status == 0 | |
return "<span class=\"label label-success\">#{@a.constantize[self.status]}</span>" | |
else | |
return "<span class=\"label label-warning\">#{@a.constantize[self.status]}</span>" | |
end | |
end | |
def getGroupsText | |
t = "" | |
self.admin_group.each do |g| | |
t += "<span class=\"label label-important\">#{g.title}</span> " | |
end | |
self.visitor_group.each do |g| | |
t += "<span class=\"label label-warning\">#{g.title}</span> " | |
end | |
return t | |
end | |
# response by message_id | |
def responses_by_message_id(message_id) | |
self.responses.where({:message_id => message_id,:response_id => nil}) | |
end | |
# get user cache permissions object for site | |
# type: visit or admin permissions | |
def get_cache(site_id, type = "visit") | |
cache = instance_variable_get("@user#{self.id}_cache_#{site_id}") | |
cache = self.cache_role.where(:mode => type, :site_id => site_id).first if cache.nil? | |
unless cache.present? | |
cache = Admin::CacheRole.new | |
cache.category_ids = "" | |
cache.page_ids = "" | |
end | |
instance_variable_set("@user#{self.id}_cache_#{site_id}", cache) | |
cache | |
end | |
# bookmarks by site | |
def bookmarks(site) | |
Plugins::Bookmark.where(:site_id => site.id, :user_id => self.id) | |
end | |
def auto_assign_to_sites(site = nil) | |
if site.present? | |
site.assigned_user.create(user_id: self.id) unless site.assigned_user.where(user_id: self.id).present? | |
auto_assign_to_group(site) | |
else | |
Admin::Site.all.each do |site| | |
site.assigned_user.create(user_id: self.id) unless site.assigned_user.where(user_id: self.id).present? | |
auto_assign_to_group(site) | |
end | |
end | |
end | |
def auto_assign_to_group(site) | |
site.admin_group.autosave.all.each do |group| | |
self.assignToGroup(group, site) unless group.assigned_group.where(assigned_user_id: self.id).present? | |
end | |
site.visitor_group.autosave.all.each do |group| | |
self.assignToGroup(group, site) unless group.assigned_group.where(assigned_user_id: self.id).present? | |
end | |
end | |
private | |
def save_date_status | |
date = (self.status.to_i == 0) ? '' : Date.today | |
self.date_lower = date | |
end | |
#return Assigned_user object to site object | |
def get_assignedToSite(site) | |
Admin::AssignedUser.where({:user_id => self.id, :site_id => site.id}).first | |
end | |
#deprecated | |
def assign_to_default_group | |
Admin::Site.all.each do |site| | |
default_group = site.get_default_visitor_group | |
as = default_group.assigned_user.create(user_id: self.id) | |
end | |
end | |
def encrypt_password | |
if !self.password.blank? | |
self.salt = make_salt if new_record? | |
self.encrypted_password = encrypt(password) | |
else | |
self.salt = read_attribute(:salt) | |
self.encrypted_password = read_attribute(:encrypted_password) | |
end | |
end | |
def encrypt(string) | |
secure_hash("#{salt}--#{string}") | |
end | |
def make_salt | |
secure_hash("#{Time.now.utc}--#{password}") | |
end | |
def secure_hash(string) | |
Digest::SHA2.hexdigest(string + "hrh") | |
end | |
def re_assign_owner | |
to = Admin::User.super_admin.first | |
Admin::Page.update_all(owner_id: to.id) | |
Admin::PageDraft.update_all(owner_id: to.id) | |
Admin::PageVersion.update_all(owner_id: to.id) | |
self.page_comment.update_all(user_id: to.id) | |
self.owner_categories.update_all(owner_id: to.id) | |
Plugins::AccordionsManager::Accordion.update_all(user_id: to.id) | |
Plugins::FormTemplate::Formpage.update_all(user_id: to.id) | |
Plugins::Messages::Message.update_all(user_id: to.id) | |
Plugins::Messages::MessagesUsers.update_all(user_id: to.id) | |
Plugins::Messages::Response.update_all(user_id: to.id) | |
Plugins::PhotoGallery::Album.update_all(user_id: to.id) | |
Plugins::TabsManager::Tab.update_all(user_id: to.id) | |
# delete user notifications | |
Plugins::EmailNotification.where("FIND_IN_SET(#{self.id}, users_id)").each do |email_notif| | |
email_notif.update(users_id: email_notif.users_id.split(",").delete_item(self.id).join(",")) | |
end | |
# destroy revisers | |
Admin::EavAttribute.where("field_type = 'select_users' or field_type='multiple_users'").each do |attr| | |
attr.eav_value.where("eav_values.value = '#{self.id}'").destroy_all | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment