-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add managing role system for chats
- Loading branch information
Showing
9 changed files
with
143 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ChatMembersService | ||
class ChangeRole < ApplicationService | ||
def call(target_chat_member, chat_member, role) | ||
return failure('You cannot grant the role of owner to another user') if role == 'owner' | ||
|
||
if can_change_role?(chat_member, target_chat_member) | ||
update_role(target_chat_member, role) | ||
else | ||
failure('You cannot change user permissions') | ||
end | ||
end | ||
|
||
private | ||
|
||
def can_change_role?(chat_member, target_chat_member) | ||
ChatMemberPolicy.new(chat_member.user, chat_member).owner? && | ||
target_chat_member.user != chat_member.user | ||
end | ||
|
||
def update_role(target_chat_member, role) | ||
target_chat_member.role = role | ||
if target_chat_member.save | ||
success({ text: 'User role was successfully updated' }) | ||
else | ||
failure('Error while updating user role') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ChatMembersService | ||
class Destroy < ApplicationService | ||
def call(target_chat_member, chat_member) | ||
if can_destroy?(chat_member, target_chat_member) | ||
target_chat_member.destroy | ||
success({ text: destroy_message(chat_member, target_chat_member) }) | ||
else | ||
failure('You cannot delete this member') | ||
end | ||
end | ||
|
||
private | ||
|
||
def can_destroy?(chat_member, target_chat_member) | ||
policy = ChatMemberPolicy.new(chat_member.user, chat_member) | ||
(policy.owner? && target_chat_member != chat_member) || | ||
(policy.admin? && target_chat_member.role != 'admin') || | ||
(policy.member? && target_chat_member == chat_member) | ||
end | ||
|
||
def destroy_message(chat_member, target_chat_member) | ||
if chat_member == target_chat_member | ||
"You've left the chat" | ||
else | ||
"You've deleted the user from the chat" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<h1>Edit Role for <%= @target_chat_member.user.name %></h1> | ||
|
||
<%= form_for [@workspace, @chat, @target_chat_member], url: workspace_chat_chat_member_path(@workspace, @chat, @target_chat_member), method: :patch do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :role %> | ||
<%= f.select :role, options_for_select([['Member', 'member'], ['Admin', 'admin']], @target_chat_member.role), {}, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= f.submit 'Update Role', class: 'btn btn-primary' %> | ||
</div> | ||
<% end %> | ||
|
||
<%= link_to 'Back to Members List', workspace_chat_chat_members_path(@workspace, @chat), class: 'btn btn-secondary' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters