Skip to content

Commit e47e5df

Browse files
vswamidass-sfdcdependabot[bot]theswamis
authored
Add assistant users (#209)
* Bump ruby-saml from 1.18.0 to 1.18.1 Bumps [ruby-saml](https://github.com/saml-toolkits/ruby-saml) from 1.18.0 to 1.18.1. - [Release notes](https://github.com/saml-toolkits/ruby-saml/releases) - [Changelog](https://github.com/SAML-Toolkits/ruby-saml/blob/master/CHANGELOG.md) - [Commits](SAML-Toolkits/ruby-saml@v1.18.0...v1.18.1) --- updated-dependencies: - dependency-name: ruby-saml dependency-version: 1.18.1 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <[email protected]> * add assistant user perms * sec updates --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Vijay Swamidass <[email protected]>
1 parent 8daea42 commit e47e5df

File tree

15 files changed

+194
-8
lines changed

15 files changed

+194
-8
lines changed

Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ GEM
271271
net-protocol
272272
nio4r (2.7.4)
273273
nkf (0.2.0)
274-
nokogiri (1.18.8-arm64-darwin)
274+
nokogiri (1.18.9-arm64-darwin)
275275
racc (~> 1.4)
276-
nokogiri (1.18.8-x86_64-darwin)
276+
nokogiri (1.18.9-x86_64-darwin)
277277
racc (~> 1.4)
278-
nokogiri (1.18.8-x86_64-linux-gnu)
278+
nokogiri (1.18.9-x86_64-linux-gnu)
279279
racc (~> 1.4)
280280
nori (2.7.1)
281281
bigdecimal
@@ -430,7 +430,7 @@ GEM
430430
parser (>= 3.3.7.2)
431431
prism (~> 1.4)
432432
ruby-progressbar (1.13.0)
433-
ruby-saml (1.18.0)
433+
ruby-saml (1.18.1)
434434
nokogiri (>= 1.13.10)
435435
rexml
436436
rubyzip (2.4.1)
@@ -488,7 +488,7 @@ GEM
488488
railties (>= 7.0.0)
489489
tailwindcss-rails (2.7.9-x86_64-linux)
490490
railties (>= 7.0.0)
491-
thor (1.3.2)
491+
thor (1.4.0)
492492
tiktoken_ruby (0.0.11.1-arm64-darwin)
493493
tiktoken_ruby (0.0.11.1-x86_64-darwin)
494494
tiktoken_ruby (0.0.11.1-x86_64-linux)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class AssistantUsersController < ApplicationController
2+
before_action :set_assistant_user, only: %i[destroy]
3+
4+
def new
5+
@assistant = Assistant.find(params[:assistant_id])
6+
@assistant_user = @assistant.assistant_users.build
7+
8+
authorize @assistant_user
9+
end
10+
11+
def index
12+
@assistant = Assistant.find(params[:assistant_id])
13+
@users = @assistant.users
14+
15+
respond_to do |format|
16+
format.html # renders users.html.erb
17+
format.json { render json: @users }
18+
end
19+
end
20+
21+
def create
22+
@assistant = Assistant.find(params[:assistant_id])
23+
@assistant_user = @assistant.assistant_users.build(assistant_user_params)
24+
25+
authorize @assistant_user
26+
27+
if @assistant_user.save
28+
redirect_to assistant_assistant_users_path(@assistant), notice: 'Assistant user was successfully created.'
29+
else
30+
render :new
31+
end
32+
end
33+
34+
def destroy
35+
authorize @assistant_user
36+
37+
@assistant_user.destroy!
38+
39+
respond_to do |format|
40+
format.html { redirect_to assistant_assistant_users_path(@assistant_user.assistant_id), notice: 'Assistant user removed.' }
41+
format.json { head :no_content }
42+
end
43+
end
44+
45+
private
46+
47+
def set_assistant_user
48+
@assistant_user = AssistantUser.find_by(user_id: params[:id], assistant_id: params[:assistant_id])
49+
end
50+
51+
def assistant_user_params
52+
params.require(:assistant_user).permit(:user_id)
53+
end
54+
end

app/controllers/assistants_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class AssistantsController < BaseAssistantsController
2-
before_action :set_assistant, only: %i[show edit update destroy]
2+
before_action :set_assistant, only: %i[show edit update destroy users]
33

44
# GET /assistants/1 or /assistants/1.json
55
def show
@@ -26,4 +26,9 @@ def clone
2626
# Render the 'new' view, which will now be used for cloning/editing
2727
render :new
2828
end
29+
30+
# GET /assistants/1/users
31+
def users
32+
redirect_to assistant_assistant_users_path(@assistant)
33+
end
2934
end

app/controllers/base_assistants_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def update
4949

5050
# DELETE /assistants/1 or /assistants/1.json
5151
def destroy
52+
authorize @assistant
53+
5254
@assistant.destroy!
5355

5456
respond_to do |format|

app/models/assistant.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class Assistant < ApplicationRecord
44
has_many :chats, dependent: :destroy
55
belongs_to :user
66
belongs_to :library, optional: true
7+
8+
has_many :assistant_users, dependent: :destroy
9+
has_many :users, through: :assistant_users
710
enum status: { development: 0, ready: 1 }
811
validates :name, presence: true
912
validates :slack_channel_name, uniqueness: true, allow_blank: true

app/models/assistant_user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AssistantUser < ApplicationRecord
2+
belongs_to :user
3+
belongs_to :assistant
4+
5+
enum role: { admin: 0, editor: 1 }
6+
end

app/models/user.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class User < ApplicationRecord
1313
has_many :owned_libraries, class_name: 'Library', foreign_key: 'user_id'
1414
has_many :owned_assistants, class_name: 'Assistant', foreign_key: 'user_id'
1515

16+
has_many :assistant_users
17+
has_many :assistants, through: :assistant_users
18+
1619
private
1720

1821
def password_strength

app/policies/assistant_policy.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ def create?
1414
end
1515

1616
def update?
17-
user.admin? || user.editor? || assistant.user_id == user.id
17+
user.admin? || user.editor? || assistant.user_id == user.id || user_is_assistant_editor?
18+
end
19+
20+
def edit?
21+
update?
22+
end
23+
24+
private
25+
26+
def user_is_assistant_editor?
27+
assistant_user = AssistantUser.find_by(user: user, assistant: assistant)
28+
assistant_user&.editor? || assistant_user&.admin?
1829
end
1930

2031
def destroy?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
class AssistantUserPolicy < ApplicationPolicy
4+
attr_reader :user, :assistant_user
5+
6+
def initialize(user, assistant_user)
7+
@user = user
8+
@assistant_user = assistant_user
9+
end
10+
11+
# Allow assistant owner to update other users on the assistant
12+
def create?
13+
user.admin? || user.editor? || @assistant_user.assistant.user_id == @user.id
14+
end
15+
16+
def destroy?
17+
create?
18+
end
19+
20+
def update?
21+
false
22+
end
23+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%= render partial: 'shared/breadcrumb', locals: { breadcrumbs: [['Home', root_path], [@assistant.name, assistant_path(@assistant)], ['Users', nil]] } %>
2+
3+
<div class="flex justify-between items-center py-2">
4+
<h1 class="font-bold text-3xl text-sky-800">Users</h1>
5+
<% if policy(@assistant).update? %>
6+
<%= render partial: 'shared/button_group', locals: { buttons: { "New" => new_assistant_assistant_user_path(@assistant) } } %>
7+
<% end %>
8+
</div>
9+
<div class="bg-white shadow-md rounded-lg overflow-hidden">
10+
<div class="grid grid-cols-3 gap-4 p-4 bg-gray-200 font-semibold">
11+
<div>Email</div>
12+
<div>Role</div>
13+
<div>Actions</div>
14+
</div>
15+
<% @users.each do |user| %>
16+
<div class="grid grid-cols-3 gap-4 p-4 border-b border-gray-200">
17+
<div><%= user.email %></div>
18+
<div><%= user.assistant_users.find_by(assistant: @assistant).role.capitalize %></div>
19+
<div>
20+
<% if policy(@assistant).update? %>
21+
<%= button_to 'Delete', assistant_assistant_user_path(@assistant, user), method: :delete, data: { confirm: 'Are you sure?' }, class: "text-red-600" %>
22+
<% end %>
23+
</div>
24+
</div>
25+
<% end %>
26+
</div>

0 commit comments

Comments
 (0)