2019-09-25 16:37:42 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Check the access policies for API::MembersController and API::UsersController
|
2015-05-05 03:10:25 +02:00
|
|
|
class UserPolicy < ApplicationPolicy
|
2019-09-25 16:37:42 +02:00
|
|
|
# Defines the scope of the users index, depending on the role of the current user
|
2015-05-05 03:10:25 +02:00
|
|
|
class Scope < Scope
|
|
|
|
def resolve
|
2019-01-14 12:57:31 +01:00
|
|
|
if user.admin?
|
2019-06-05 12:11:51 +02:00
|
|
|
scope.includes(:group, :training_credits, :machine_credits, statistic_profile: [subscriptions: [plan: [:credits]]], profile: [:user_avatar])
|
2019-01-21 15:17:56 +01:00
|
|
|
.joins(:roles).where("users.is_active = 'true' AND roles.name = 'member'").order('users.created_at desc')
|
2015-05-05 03:10:25 +02:00
|
|
|
else
|
2019-01-21 15:17:56 +01:00
|
|
|
scope.includes(profile: [:user_avatar]).joins(:roles).where("users.is_active = 'true' AND roles.name = 'member'")
|
|
|
|
.where(is_allow_contact: true).order('users.created_at desc')
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show?
|
2020-04-27 17:48:13 +02:00
|
|
|
user.admin? || user.manager? || (record.is_allow_contact && record.member?) || (user.id == record.id)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
2020-04-27 17:48:13 +02:00
|
|
|
user.admin? || user.manager? || (user.id == record.id)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def destroy?
|
2019-10-29 09:59:21 +01:00
|
|
|
user.admin? || (user.id == record.id)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2020-04-27 17:48:13 +02:00
|
|
|
%w[merge complete_tour].each do |action|
|
|
|
|
define_method "#{action}?" do
|
|
|
|
user.id == record.id
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2016-05-30 15:39:19 +02:00
|
|
|
|
2020-04-28 15:34:17 +02:00
|
|
|
%w[list index create_member].each do |action|
|
2020-04-27 17:48:13 +02:00
|
|
|
define_method "#{action}?" do
|
|
|
|
user.admin? || user.manager?
|
|
|
|
end
|
2020-02-18 17:36:45 +01:00
|
|
|
end
|
|
|
|
|
2020-04-27 17:48:13 +02:00
|
|
|
%w[create mapping].each do |action|
|
2016-06-21 14:39:44 +02:00
|
|
|
define_method "#{action}?" do
|
2019-01-14 12:57:31 +01:00
|
|
|
user.admin?
|
2016-06-21 14:39:44 +02:00
|
|
|
end
|
2016-05-30 15:39:19 +02:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|