From 0e5b5e8965a9e1604047300f5621727c7d55e993 Mon Sep 17 00:00:00 2001 From: Ayushi Sharma Date: Tue, 16 Jul 2024 14:15:20 +0530 Subject: [PATCH 1/4] feat(group): added remove_user and check user_in_group api --- app/controllers/api/v1/groups_controller.rb | 27 +++++++++++++++++++++ config/routes.rb | 4 ++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index 14bef8c1..ca34f0cf 100755 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -40,6 +40,33 @@ def add_user head :no_content end + def remove_user + @group = Group.find_by(id: params[:id]) + return head :not_found unless @group.present? + + return raise_unauthorized unless current_user.admin? || @group.admin?(current_user) + + user = User.find_by(id: params[:user_id]) + return head :unprocessable_entity unless user.present? + + @group.remove_user(params[:user_id]) + head :no_content + end + + def user_in_group + @group = Group.find_by(id: params[:id]) + return head :not_found unless @group.present? + + user = User.find_by(id: params[:user_id]) + if user.nil? + render json: { error: "user not found" }, status: :not_found + return + end + + is_member = @group.users.exists?(user.id) + render json: { is_member: is_member } + end + private def group_params diff --git a/config/routes.rb b/config/routes.rb index c4401ac5..cf967e72 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,7 +87,9 @@ post 'endpoints' => 'endpoints#create', format: :json, :constraints => { format: 'json' } post 'endpoints/:id/add_group' => 'endpoints#add_group', format: :json, constraints: { format: 'json' } post 'groups/:id/users' => 'groups#add_user', format: :json, constraints: { format: 'json' } - + delete 'groups/:id/users/:user_id/remove' => 'groups#remove_user', format: :json, constraints: { format: 'json' } + get '/groups/:id/user_in_group/:user_id' => 'groups#user_in_group', format: :json, constraints: { format: 'json' } + resources :groups, only: [:create], format: :json resources :vpns, only: [:create], format: :json do member do From 7646b7427cee3d8890c1429ef6dce8d54a381870 Mon Sep 17 00:00:00 2001 From: Ayushi Sharma Date: Tue, 16 Jul 2024 21:59:26 +0530 Subject: [PATCH 2/4] feat(api): get group member API --- app/controllers/api/v1/groups_controller.rb | 14 +++++++++----- config/routes.rb | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index ca34f0cf..b8203b3f 100755 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -53,18 +53,22 @@ def remove_user head :no_content end - def user_in_group + def get_group_member @group = Group.find_by(id: params[:id]) return head :not_found unless @group.present? - + user = User.find_by(id: params[:user_id]) if user.nil? - render json: { error: "user not found" }, status: :not_found + render json: { error: "User not found" }, status: :not_found return end - + is_member = @group.users.exists?(user.id) - render json: { is_member: is_member } + if is_member + render json: user.as_json(only: [:id, :email, :created_at, :updated_at, :uid, :name, :active]), status: :ok + else + render json: { error: "User not a member of the group" }, status: :not_found + end end private diff --git a/config/routes.rb b/config/routes.rb index cf967e72..bd0dfc13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -88,7 +88,7 @@ post 'endpoints/:id/add_group' => 'endpoints#add_group', format: :json, constraints: { format: 'json' } post 'groups/:id/users' => 'groups#add_user', format: :json, constraints: { format: 'json' } delete 'groups/:id/users/:user_id/remove' => 'groups#remove_user', format: :json, constraints: { format: 'json' } - get '/groups/:id/user_in_group/:user_id' => 'groups#user_in_group', format: :json, constraints: { format: 'json' } + get '/groups/:id/members/:user_id' => 'groups#get_group_member', format: :json, constraints: { format: 'json' } resources :groups, only: [:create], format: :json resources :vpns, only: [:create], format: :json do From ddcdf307af536afaeb0a4cad791f8e6e44232581 Mon Sep 17 00:00:00 2001 From: Ayushi Sharma Date: Tue, 30 Jul 2024 00:56:11 +0530 Subject: [PATCH 3/4] feat: remove get group user API --- app/controllers/api/v1/groups_controller.rb | 18 ------------------ config/routes.rb | 3 +-- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index b8203b3f..ed67806c 100755 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -53,24 +53,6 @@ def remove_user head :no_content end - def get_group_member - @group = Group.find_by(id: params[:id]) - return head :not_found unless @group.present? - - user = User.find_by(id: params[:user_id]) - if user.nil? - render json: { error: "User not found" }, status: :not_found - return - end - - is_member = @group.users.exists?(user.id) - if is_member - render json: user.as_json(only: [:id, :email, :created_at, :updated_at, :uid, :name, :active]), status: :ok - else - render json: { error: "User not a member of the group" }, status: :not_found - end - end - private def group_params diff --git a/config/routes.rb b/config/routes.rb index bd0dfc13..82e780c2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,8 +87,7 @@ post 'endpoints' => 'endpoints#create', format: :json, :constraints => { format: 'json' } post 'endpoints/:id/add_group' => 'endpoints#add_group', format: :json, constraints: { format: 'json' } post 'groups/:id/users' => 'groups#add_user', format: :json, constraints: { format: 'json' } - delete 'groups/:id/users/:user_id/remove' => 'groups#remove_user', format: :json, constraints: { format: 'json' } - get '/groups/:id/members/:user_id' => 'groups#get_group_member', format: :json, constraints: { format: 'json' } + delete 'groups/:id/users/:user_id' => 'groups#remove_user', format: :json, constraints: { format: 'json' } resources :groups, only: [:create], format: :json resources :vpns, only: [:create], format: :json do From 6fb6b6f11406067987c6b79d44a698a1b1376a52 Mon Sep 17 00:00:00 2001 From: Rahmat Hidayat Date: Tue, 30 Jul 2024 10:25:00 +0700 Subject: [PATCH 4/4] feat: expose groups and vpns related list APIs (#1) * feat: expose groups and vpns related list APIs * feat: filter active user only for group admins API --- Gemfile | 1 + app/controllers/api/v1/groups_controller.rb | 19 +++++++++++++++++++ app/controllers/api/v1/vpns_controller.rb | 11 +++++++++++ config/routes.rb | 9 ++++++--- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 904b1069..9bf521b8 100755 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem 'slim-rails' gem 'turbolinks' gem 'uglifier' gem 'whenever', require: false +gem 'kaminari' group :development, :test do gem 'capybara' diff --git a/app/controllers/api/v1/groups_controller.rb b/app/controllers/api/v1/groups_controller.rb index 14bef8c1..cc827d08 100755 --- a/app/controllers/api/v1/groups_controller.rb +++ b/app/controllers/api/v1/groups_controller.rb @@ -1,4 +1,9 @@ class ::Api::V1::GroupsController < ::Api::V1::BaseController + def index + groups = Group.order(:id).page(params[:page]).per(params[:per_page]) + render json: groups, status: :ok + end + def create if current_user.admin? @group = Group.new(group_params) @@ -40,6 +45,20 @@ def add_user head :no_content end + def list_admins + group = Group.find(params[:id]) + users = group.group_admins.joins(:user). + select('users.id, users.email, users.name, users.active, group_admins.created_at as join_date'). + where('users.active = ?', true) + render json: users, status: :ok + end + + def associated_vpns + group = Group.find(params[:id]) + vpns = group.vpns + render json: vpns, status: :ok + end + private def group_params diff --git a/app/controllers/api/v1/vpns_controller.rb b/app/controllers/api/v1/vpns_controller.rb index 3b0d3353..4e98803c 100755 --- a/app/controllers/api/v1/vpns_controller.rb +++ b/app/controllers/api/v1/vpns_controller.rb @@ -1,6 +1,17 @@ class ::Api::V1::VpnsController < ::Api::V1::BaseController before_action :set_vpn, only: [:assign_group] + def index + vpns = Vpn.order(:id).page(params[:page]).per(params[:per_page]) + render json: vpns, status: :ok + end + + def associated_groups + vpn = Vpn.find(params[:id]) + groups = vpn.groups + render json: groups, status: :ok + end + def create if current_user.admin? @vpn = Vpn.new(vpn_params) diff --git a/config/routes.rb b/config/routes.rb index c4401ac5..f32759b3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,9 +87,12 @@ post 'endpoints' => 'endpoints#create', format: :json, :constraints => { format: 'json' } post 'endpoints/:id/add_group' => 'endpoints#add_group', format: :json, constraints: { format: 'json' } post 'groups/:id/users' => 'groups#add_user', format: :json, constraints: { format: 'json' } - - resources :groups, only: [:create], format: :json - resources :vpns, only: [:create], format: :json do + get 'groups/:id/admins' => 'groups#list_admins', format: :json, constraints: { format: 'json' } + get 'groups/:id/vpns' => 'groups#associated_vpns', format: :json, constraints: { format: 'json' } + get 'vpns/:id/groups' => 'vpns#associated_groups', format: :json, constraints: { format: 'json' } + + resources :groups, only: [:create, :index], format: :json + resources :vpns, only: [:create, :index], format: :json do member do post 'assign_group' end