orbit-basic/lib/orbit_model/authorizable.rb

57 lines
1.7 KiB
Ruby

module OrbitModel
module Authorizable
def self.included(base)
base.class_eval do
has_one :auth_approval, as: :approval_authorizable, dependent: :destroy
has_one :auth_sub_manager, as: :sub_manager_authorizable, dependent: :destroy
delegate :authorized_users, to: :auth_approval, prefix: true, allow_nil: true
delegate :authorized_users, to: :auth_manager, prefix: true, allow_nil: true
delegate :authorized_users, to: :auth_sub_manager, prefix: true, allow_nil: true
send :include, InstanceMethods
end
end
module InstanceMethods
# Normal case
# Use of categories to define approval and sub-manager
def approval_users
auth_approval_authorized_users
end
def sub_managers
auth_sub_manager_authorized_users
end
def user_can_approve?(user)
approval_users.include?(user) if approval_users
end
def user_can_sub_manage?(user)
sub_managers.include?(user) if sub_managers
end
# Specific case
# Approval or sub-manager is defined on something else than categories
def authorized_users_by_title(title)
return approval_users if auth_approval && auth_approval.title.eql?(title)
return sub_managers if auth_sub_manager && auth_sub_manager.title.eql?(title)
end
def get_authorization_by_title(title)
return auth_approval if auth_approval && auth_approval.title.eql?(title)
return auth_sub_manager if auth_sub_manager && auth_sub_manager.title.eql?(title)
end
def user_is_authorized_by_title?(user, title)
authorized_users_by_title(title).include?(user)
end
end
end
end