2013-07-02 08:46:44 +00:00
|
|
|
class Admin::AuthorizationsController < OrbitBackendController
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
prepend_before_filter :admin_or_manager
|
2013-07-02 08:46:44 +00:00
|
|
|
|
|
|
|
def index
|
2013-08-19 10:54:35 +00:00
|
|
|
@module_apps ||= ModuleApp.any_of({is_authorizable: true}, {is_approvable: true}).order_by([:title, :asc])
|
2013-07-02 08:46:44 +00:00
|
|
|
if @module_apps && @module_apps.include?(@module_app)
|
2013-07-25 09:36:55 +00:00
|
|
|
if @type
|
|
|
|
case @type
|
2013-08-19 10:54:35 +00:00
|
|
|
when 'category_authorization', 'category_approval'
|
2013-07-25 09:36:55 +00:00
|
|
|
if @module_app.has_category
|
|
|
|
@objects = @module_app.categories
|
2013-07-02 08:46:44 +00:00
|
|
|
else
|
|
|
|
@error = t(:no_category)
|
|
|
|
end
|
|
|
|
else
|
2013-07-25 09:36:55 +00:00
|
|
|
@objects = @klass.all
|
|
|
|
end
|
|
|
|
unless @objects.blank?
|
|
|
|
@object ||= @objects.first
|
2013-08-19 10:54:35 +00:00
|
|
|
@users = @object.get_authorization_by_title("#{@type}_#{@module_app.key}").authorized_users rescue nil
|
2013-07-25 09:36:55 +00:00
|
|
|
else
|
|
|
|
@error = t(:no_data)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-07-25 09:36:55 +00:00
|
|
|
else
|
2013-08-19 10:54:35 +00:00
|
|
|
@users = @module_app.managers
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
elsif @module_apps
|
|
|
|
@module_app = @module_apps.first
|
|
|
|
redirect_to admin_authorizations_url(@module_app.key)
|
|
|
|
else
|
|
|
|
redirect_to :root
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_roles
|
2013-10-15 15:38:16 +00:00
|
|
|
roles = Role.find(params[:role_ids]) rescue nil
|
|
|
|
unless roles.nil?
|
2013-08-19 10:54:35 +00:00
|
|
|
authorization = get_or_create_authorization
|
2013-10-15 15:38:16 +00:00
|
|
|
add_roles_to_auth(authorization, roles)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-08-19 10:54:35 +00:00
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_sub_roles
|
2013-10-15 15:38:16 +00:00
|
|
|
sub_roles = SubRole.find(params[:sub_role_ids]) rescue nil
|
|
|
|
unless sub_roles.nil?
|
2013-08-19 10:54:35 +00:00
|
|
|
authorization = get_or_create_authorization
|
2013-10-15 15:38:16 +00:00
|
|
|
add_sub_roles_to_auth(authorization, sub_roles)
|
2013-08-19 10:54:35 +00:00
|
|
|
end
|
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_users
|
2013-10-15 15:38:16 +00:00
|
|
|
users = User.find(params[:user_ids]) rescue nil
|
|
|
|
unless users.nil?
|
2013-08-19 10:54:35 +00:00
|
|
|
authorization = get_or_create_authorization
|
2013-10-15 15:38:16 +00:00
|
|
|
add_users_to_auth(authorization, users)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-08-19 10:54:35 +00:00
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def modal_select
|
|
|
|
existing_users = User.find(params[:ids]) rescue []
|
|
|
|
roles = Role.all
|
2013-07-25 09:36:55 +00:00
|
|
|
if @type
|
|
|
|
@object_id = @object.id if @object
|
2013-07-02 08:46:44 +00:00
|
|
|
@sorted_users = roles.inject({}) do |users, role|
|
2013-08-19 10:54:35 +00:00
|
|
|
users[role] = role.users.where(admin: false) - existing_users - @module_app.managers
|
2013-07-02 08:46:44 +00:00
|
|
|
users
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@sorted_users = roles.inject({}) do |users, role|
|
2013-08-19 10:54:35 +00:00
|
|
|
users[role] = role.users.where(admin: false) - existing_users
|
2013-07-02 08:46:44 +00:00
|
|
|
users
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def remove_roles
|
|
|
|
roles = Role.find(params[:role_ids]) rescue []
|
|
|
|
unless roles.blank?
|
|
|
|
authorization = get_or_create_authorization
|
|
|
|
remove_roles_form_auth(authorization, roles)
|
|
|
|
end
|
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_sub_roles
|
|
|
|
sub_roles = SubRole.find(params[:sub_role_ids]) rescue []
|
|
|
|
unless sub_roles.blank?
|
|
|
|
authorization = get_or_create_authorization
|
|
|
|
remove_sub_roles_from_auth(authorization, sub_roles)
|
|
|
|
end
|
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
def remove_users
|
|
|
|
@users = User.find(params[:ids]) rescue []
|
|
|
|
unless @users.blank?
|
2013-08-19 10:54:35 +00:00
|
|
|
authorization = get_or_create_authorization
|
|
|
|
remove_users_from_auth(authorization, @users)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
2013-08-19 10:54:35 +00:00
|
|
|
@users = authorization.authorized_users
|
|
|
|
render 'admin/authorizations/reload_users'
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_roles_to_auth(authorization, roles)
|
|
|
|
authorization.add_roles(roles)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_sub_roles_to_auth(authorization, sub_roles)
|
|
|
|
authorization.add_sub_roles(sub_roles)
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def add_users_to_auth(authorization, users)
|
|
|
|
authorization.add_users(users)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_or_create_authorization
|
|
|
|
case @type
|
|
|
|
when 'category_approval'
|
|
|
|
if @object
|
|
|
|
@object.auth_approval || @object.create_auth_approval(module_app_id: @module_app.id, title: "#{@type}_#{@module_app.key}")
|
|
|
|
else
|
|
|
|
@error = t(:no_data)
|
|
|
|
end
|
|
|
|
when 'category_authorization'
|
|
|
|
if @object
|
|
|
|
@object.auth_sub_manager || @object.create_auth_sub_manager(module_app_id: @module_app.id, title: "#{@type}_#{@module_app.key}")
|
|
|
|
else
|
|
|
|
@error = t(:no_data)
|
|
|
|
end
|
|
|
|
when nil
|
2013-10-15 15:38:16 +00:00
|
|
|
@module_app.auth_manager || @module_app.create_auth_manager(module_app_id: @module_app.id, title: @module_app.key)
|
2013-08-19 10:54:35 +00:00
|
|
|
else
|
|
|
|
auth = @object.get_authorization_by_title("#{@type}_#{@module_app.key}")
|
|
|
|
unless auth
|
|
|
|
auth = @object.create_auth_approval(module_app_id: @module_app.id, title: "#{@type}_#{@module_app.key}") if @type.include?('approval')
|
|
|
|
auth = @object.create_auth_sub_manager(module_app_id: @module_app.id, title: "#{@type}_#{@module_app.key}") if @type.include?('authorization')
|
|
|
|
end
|
|
|
|
auth
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
def remove_roles_form_auth(authorization, roles)
|
|
|
|
authorization.remove_roles(roles)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_sub_roles_from_auth(authorization, sub_roles)
|
|
|
|
authorization.remove_sub_roles(sub_roles)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_users_from_auth(authorization, users)
|
|
|
|
authorization.remove_users(users)
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def admin_or_manager
|
2013-08-19 10:54:35 +00:00
|
|
|
@override_can_use = true
|
2013-07-02 08:46:44 +00:00
|
|
|
setup_vars
|
|
|
|
authenticate_user!
|
|
|
|
user_is_manager?
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_vars
|
|
|
|
@module_app = ModuleApp.first(conditions: {:key => params[:module]} ) if params[:module]
|
2013-07-25 09:36:55 +00:00
|
|
|
@type = params[:type].underscore if params[:type]
|
|
|
|
if @type
|
2013-08-19 10:54:35 +00:00
|
|
|
@klass = @type.gsub('_authorization', '').gsub('_approval', '').classify.constantize rescue nil
|
2013-07-25 09:36:55 +00:00
|
|
|
@object = @klass.find(params[:id]) rescue nil
|
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_is_manager?
|
|
|
|
unless is_admin?
|
|
|
|
@module_apps = current_user.managed_module_apps
|
|
|
|
redirect_to :root if @module_apps.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|