Gab-Social/app/policies/group_policy.rb

96 lines
1.9 KiB
Ruby
Raw Normal View History

2019-07-02 07:10:25 +00:00
# frozen_string_literal: true
class GroupPolicy < ApplicationPolicy
def index?
true
end
def create?
true
end
2019-07-02 07:10:25 +00:00
def update?
if admin?
true
else
check_archive!
is_group_admin?
end
2019-07-02 07:10:25 +00:00
end
def destroy?
if admin?
true
else
check_archive!
is_group_admin?
end
2019-07-02 07:10:25 +00:00
end
def approve_status?
check_archive!
is_group_admin_or_moderator?
2019-07-02 07:10:25 +00:00
end
def destroy_status?
check_archive!
is_group_admin_or_moderator?
2019-07-02 07:10:25 +00:00
end
def join?
check_archive!
2019-07-16 14:10:38 +00:00
raise GabSocial::ValidationError, "Account is already a member of this group." if is_member?
raise GabSocial::ValidationError, "Account is removed from this group." if is_removed?
2019-07-02 07:10:25 +00:00
return true
end
def leave?
check_archive!
raise GabSocial::ValidationError, "Group member account not found." if not is_member?
is_account_the_only_admin = (is_group_admin? and record.group_accounts.where(role: :admin).count == 1)
raise GabSocial::ValidationError, "This is the last admin of this group." if is_account_the_only_admin
return true
end
def update_account?
is_group_admin_or_moderator?
2019-07-02 07:10:25 +00:00
end
2019-07-16 12:53:56 +00:00
def show_removed_accounts?
is_group_admin_or_moderator?
2019-07-16 12:53:56 +00:00
end
def create_removed_account?
is_group_admin_or_moderator?
2019-07-16 12:53:56 +00:00
end
def destroy_removed_account?
is_group_admin_or_moderator?
2019-07-16 12:53:56 +00:00
end
2019-07-02 07:10:25 +00:00
private
def is_removed?
record.group_removed_accounts.where(account_id: current_account.id).exists?
end
2019-07-02 07:10:25 +00:00
def is_member?
record.group_accounts.where(account_id: current_account.id).exists?
end
def is_group_admin?
record.group_accounts.where(account_id: current_account.id, role: :admin).exists?
end
def is_group_admin_or_moderator?
record.group_accounts.where(account_id: current_account.id, role: [:moderator, :admin]).exists?
end
2019-07-02 07:10:25 +00:00
def check_archive!
raise GabSocial::ValidationError, "This group has been archived." if record.is_archived
end
end