2012-02-09 09:48:51 +00:00
|
|
|
module OrbitCoreLib
|
2012-09-04 04:31:27 +00:00
|
|
|
module Preview
|
|
|
|
def self.included(base)
|
2012-09-07 09:55:59 +00:00
|
|
|
|
|
|
|
# base.instance_eval("field :is_preview,type: Boolean,:default => false")
|
|
|
|
# base.instance_eval("scope :not_preview,where(:is_preview=>false)")
|
|
|
|
base.class_eval ("
|
|
|
|
def to_preview
|
|
|
|
raise 'Developer,please override to_preview method'
|
|
|
|
end
|
|
|
|
")
|
2012-09-04 04:31:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-05 08:00:45 +00:00
|
|
|
module ObjectDisable
|
|
|
|
def self.included(base)
|
|
|
|
|
|
|
|
base.instance_eval("field :disable,type: Boolean,:default => false")
|
2013-03-20 09:45:08 +00:00
|
|
|
base.instance_eval("scope :all, where(:disable.in => [false, nil, ''])")
|
2012-07-05 08:00:45 +00:00
|
|
|
base.instance_eval("scope :admin_manager_all,find(:all)")
|
2012-07-06 10:11:55 +00:00
|
|
|
|
2012-07-05 08:00:45 +00:00
|
|
|
base.define_singleton_method :find do |*args|
|
|
|
|
if args ==[:all]
|
|
|
|
unscoped
|
|
|
|
else
|
2012-07-06 10:11:55 +00:00
|
|
|
res = unscoped.find(args)
|
|
|
|
res.count == 1 ? res[0] : res
|
2012-07-05 08:00:45 +00:00
|
|
|
end
|
|
|
|
end
|
2012-07-06 10:11:55 +00:00
|
|
|
|
|
|
|
base.define_singleton_method :first do |*args|
|
|
|
|
all.first
|
|
|
|
end
|
|
|
|
|
|
|
|
base.define_singleton_method :last do |*args|
|
|
|
|
all.last
|
|
|
|
end
|
|
|
|
|
2012-07-05 08:00:45 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-09 09:48:51 +00:00
|
|
|
module ObjectAuthable
|
|
|
|
def self.included(base)
|
|
|
|
base.instance_eval("has_many :object_auths,as: :obj_authable,dependent: :delete")
|
|
|
|
|
|
|
|
base.define_singleton_method :authed_for_user do |user,title = nil|
|
|
|
|
sub_role_ids_ary=user.sub_roles.collect{|t| t.id}
|
|
|
|
if title.nil?
|
|
|
|
auth_object_space = ObjectAuth.where(obj_authable_type: self.to_s)
|
|
|
|
else
|
|
|
|
auth_object_space = ObjectAuth.where(obj_authable_type: self.to_s,title: title)
|
|
|
|
end
|
|
|
|
query1 = auth_object_space.any_in({sub_role_ids: sub_role_ids_ary}).excludes(blocked_user_ids: user.id)
|
2013-06-26 02:46:50 +00:00
|
|
|
query2 = auth_object_space.any_of({all: true},{privilege_user_ids: user.id}).excludes(blocked_user_ids: user.id)
|
|
|
|
# query2 = auth_object_space.any_of({all: true},{privilege_user_ids: user.id},{role_ids: user.role_ids}).excludes(blocked_user_ids: user.id) #save for backup if something went wrong (0626 Matt)
|
|
|
|
|
2012-02-09 09:48:51 +00:00
|
|
|
result = (query1 + query2).uniq
|
2012-07-04 09:36:51 +00:00
|
|
|
result.collect{|t| t.obj_authable}.delete_if{|val| val==nil}
|
2012-02-09 09:48:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-05-15 10:55:16 +00:00
|
|
|
def cur_user_is_sub_manager_of(title)
|
|
|
|
authed_users(title).include?(User.current)
|
|
|
|
end
|
|
|
|
|
2012-10-16 06:23:44 +00:00
|
|
|
def module_app
|
2012-05-14 04:34:15 +00:00
|
|
|
ModuleApp.first(conditions: {:title => self.class::APP_NAME} )
|
|
|
|
end
|
|
|
|
|
2012-05-11 08:44:40 +00:00
|
|
|
def pp_object
|
|
|
|
"Object Auth method 'pp_object' need to be defined for class #{self.class}"
|
|
|
|
end
|
|
|
|
|
2012-04-27 10:38:21 +00:00
|
|
|
def get_object_auth_by_title(title)
|
2012-05-22 03:17:50 +00:00
|
|
|
oa = self.object_auths.where({title: title }).first
|
2013-07-02 08:46:44 +00:00
|
|
|
if oa.nil? #&& (self.class::ObjectAuthTitlesOptions.include? title)
|
2012-05-22 08:52:12 +00:00
|
|
|
oa = self.object_auths.create title: title
|
2012-05-22 03:17:50 +00:00
|
|
|
end
|
|
|
|
oa
|
2012-04-27 10:38:21 +00:00
|
|
|
end
|
|
|
|
|
2012-02-09 09:48:51 +00:00
|
|
|
def authed_users(title=nil)
|
|
|
|
users = []
|
2012-03-22 06:33:59 +00:00
|
|
|
users = case title
|
|
|
|
when :all
|
|
|
|
ary = self.object_auths.collect{|t| t.auth_users}
|
|
|
|
ary.flatten!
|
|
|
|
when nil
|
|
|
|
if self.object_auths.count ==1
|
|
|
|
self.object_auths.first.auth_users_after_block_list rescue []
|
|
|
|
else
|
|
|
|
logger.info "Warning calling a auth commend without specificed value( has multi-auths ), return empty"
|
|
|
|
[]
|
|
|
|
end
|
2012-02-09 09:48:51 +00:00
|
|
|
else
|
2012-04-27 10:38:21 +00:00
|
|
|
get_object_auth_by_title(title).auth_users rescue []
|
2012-02-09 09:48:51 +00:00
|
|
|
end
|
|
|
|
users
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
module ObjectTokenUtility
|
2012-02-16 05:57:28 +00:00
|
|
|
def self.included(base)
|
|
|
|
base.instance_eval("field :s_token")
|
|
|
|
base.instance_eval("after_create :generate_token")
|
|
|
|
end
|
|
|
|
|
|
|
|
def token
|
|
|
|
return self.s_token
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def generate_token
|
2012-12-27 04:14:59 +00:00
|
|
|
self.s_token = SecureRandom.hex(16)
|
2012-02-16 05:57:28 +00:00
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
end
|
2012-02-15 10:20:44 +00:00
|
|
|
|
2012-12-03 10:52:36 +00:00
|
|
|
module PermissionUtility
|
2012-02-15 10:20:44 +00:00
|
|
|
private
|
|
|
|
def check_permission(type = :use)
|
2012-08-23 08:05:14 +00:00
|
|
|
permission_grant = current_or_guest_user.admin?? true : false
|
2012-02-16 05:57:28 +00:00
|
|
|
module_app = @module_app.nil?? find_module_app_by_token(params[:token]) : @module_app
|
2012-02-15 10:20:44 +00:00
|
|
|
unless permission_grant
|
|
|
|
permission_grant = case type
|
|
|
|
when :use
|
|
|
|
users_ary = module_app.app_auth.auth_users rescue nil
|
|
|
|
users_ary = [] if users_ary.nil?
|
2012-08-23 08:05:14 +00:00
|
|
|
(users_ary.include?(current_or_guest_user) || module_app.is_manager?(current_or_guest_user) || module_app.is_sub_manager?(current_or_guest_user))
|
2012-02-15 10:20:44 +00:00
|
|
|
when :manager
|
2012-08-23 08:05:14 +00:00
|
|
|
module_app.is_manager?(current_or_guest_user)
|
2012-02-15 10:20:44 +00:00
|
|
|
when :sub_manager
|
2012-08-23 08:05:14 +00:00
|
|
|
module_app.is_manager?(current_or_guest_user) || module_app.is_sub_manager?(current_or_guest_user)
|
2012-02-15 10:20:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
permission_grant
|
|
|
|
end
|
2012-02-16 05:57:28 +00:00
|
|
|
def find_module_app_by_token(token)
|
|
|
|
ModuleApp.first(conditions: {s_token: token})
|
|
|
|
end
|
2012-02-15 10:20:44 +00:00
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
module Authorization
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
before_filter :can_use
|
2013-08-22 04:17:50 +00:00
|
|
|
send :include, InstanceMethods
|
2013-08-19 10:54:35 +00:00
|
|
|
end
|
2013-08-22 04:17:50 +00:00
|
|
|
base.extend(ClassMethods)
|
2013-08-19 10:54:35 +00:00
|
|
|
end
|
|
|
|
|
2013-08-22 04:17:50 +00:00
|
|
|
module ClassMethods
|
|
|
|
protected
|
|
|
|
|
|
|
|
def open_for_admin(arg = nil)
|
|
|
|
if arg
|
|
|
|
key = arg.shift
|
|
|
|
prepend_before_filter key[0] => key[1] {|f| f.open_for :admin}
|
|
|
|
else
|
|
|
|
prepend_before_filter {|f| f.open_for :admin}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_for_manager(arg = nil)
|
|
|
|
if arg
|
|
|
|
key = arg.shift
|
|
|
|
prepend_before_filter key[0] => key[1] {|f| f.open_for :manager}
|
|
|
|
else
|
|
|
|
prepend_before_filter {|f| f.open_for :manager}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_for_sub_manager(arg = nil)
|
|
|
|
if arg
|
|
|
|
key = arg.shift
|
|
|
|
prepend_before_filter key[0] => key[1] {|f| f.open_for :sub_manager}
|
|
|
|
else
|
|
|
|
prepend_before_filter {|f| f.open_for :sub_manager}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_for_approver(arg = nil)
|
|
|
|
if arg
|
|
|
|
key = arg.shift
|
|
|
|
prepend_before_filter key[0] => key[1] {|f| f.open_for :approver}
|
|
|
|
else
|
|
|
|
prepend_before_filter {|f| f.open_for :approver}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_for_visitor(arg = nil)
|
|
|
|
if arg
|
|
|
|
key = arg.shift
|
|
|
|
prepend_before_filter key[0] => key[1] {|f| f.open_for :visitor}
|
|
|
|
else
|
|
|
|
prepend_before_filter {|f| f.open_for :visitor}
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
|
2013-08-22 04:17:50 +00:00
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
|
2013-08-22 04:17:50 +00:00
|
|
|
module InstanceMethods
|
|
|
|
protected
|
|
|
|
def can_use
|
2013-08-23 04:08:33 +00:00
|
|
|
setup_vars
|
|
|
|
set_current_user
|
2013-08-22 04:17:50 +00:00
|
|
|
if @user_type
|
|
|
|
@user_type.each do |user_type|
|
|
|
|
open = false
|
|
|
|
visitor = false
|
|
|
|
case user_type
|
|
|
|
when :admin
|
|
|
|
open ||= check_admin
|
|
|
|
when :manager
|
|
|
|
open ||= check_manager
|
|
|
|
when :sub_manager
|
|
|
|
open ||= check_sub_manager
|
|
|
|
when :approver
|
|
|
|
open ||= check_sub_manager
|
|
|
|
when :visitor
|
|
|
|
open ||= true
|
|
|
|
visitor ||= true
|
|
|
|
end
|
|
|
|
check_backend_openness if visitor
|
|
|
|
authenticate_user! unless visitor
|
|
|
|
redirect_to root_url unless open
|
|
|
|
end
|
|
|
|
else
|
2013-08-19 10:54:35 +00:00
|
|
|
authenticate_user!
|
|
|
|
check_user_can_use
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
|
2013-08-22 04:17:50 +00:00
|
|
|
def check_admin
|
|
|
|
current_or_guest_user.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manager
|
|
|
|
check_admin || @module_app.is_manager?(current_or_guest_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_sub_manager
|
|
|
|
check_admin || check_manager || @module_app.is_sub_manager?(current_or_guest_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_approver
|
|
|
|
check_admin || check_manager || @module_app.can_approve?(current_or_guest_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_for(var)
|
|
|
|
@user_type ||= []
|
|
|
|
@user_type << var
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_user_can_use
|
|
|
|
unless current_or_guest_user.admin? || @module_app.is_manager?(current_or_guest_user) || @module_app.is_sub_manager?(current_or_guest_user) || @module_app.can_approve?(current_or_guest_user)
|
|
|
|
redirect_to root_url
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 10:52:36 +00:00
|
|
|
|
2013-08-22 04:17:50 +00:00
|
|
|
def setup_vars
|
|
|
|
@app_title ||= controller_path.split('/')[1].singularize
|
|
|
|
@module_app ||= ModuleApp.first(conditions: {:key => @app_title} )
|
|
|
|
# raise ModuleAppError, 'Can not find ModuleApp' if @module_app.nil?
|
2012-12-03 10:52:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-09 09:48:51 +00:00
|
|
|
end
|