Skip to content

Commit

Permalink
feat: expose list resources and manage access APIs (#5)
Browse files Browse the repository at this point in the history
* 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

* feat(group): membership management APIs (#2)

* feat(group): added remove_user and check user_in_group api

* feat(api): get group member API

* feat: remove get group user API

---------

Co-authored-by: Ayushi Sharma <[email protected]>
Co-authored-by: Rahmat Hidayat <[email protected]>

* feat: enhance list groups and vpns APIs (#4)

* chore: return 404 if group not found

* feat: introduce search query in list vpns

* fix: return 404 if vpn not found

* fix: return 404 if group not found

* fix: use find_by_id instead of find

* feat: introduce search query in list vpn groups

* ci: docker release (#3)

* ci: test docker release action

* ci: change trigger

* chore: lock bundler version

* fix: fix docker push command

* ci: change trigger

* ci: run release only on tag

---------

Co-authored-by: Ayushi Sharma <[email protected]>
Co-authored-by: Ayushi Sharma <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2024
1 parent 9c863c5 commit 23d06c6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Docker Image CI

on:
push:
tags:
- '*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build . --file Dockerfile --tag gotocompany/gate:${{ github.ref_name }}
- name: Login to DockerHub
run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }}
- name: Push Docker image
run: docker push gotocompany/gate:${{ github.ref_name }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /app
COPY Gemfile /app
COPY Gemfile.lock /app

RUN gem install bundler -v '>= 2.0'
RUN gem install bundler -v '2.0.2'
RUN bundle install --without development
COPY . /app

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem 'slim-rails'
gem 'turbolinks'
gem 'uglifier'
gem 'whenever', require: false
gem 'kaminari'

group :development, :test do
gem 'capybara'
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/api/v1/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -40,6 +45,37 @@ 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 list_admins
group = Group.find_by_id(params[:id])
return head :not_found unless group.present?

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_by_id(params[:id])
return head :not_found unless group.present?

vpns = group.vpns
render json: vpns, status: :ok
end

private

def group_params
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/api/v1/vpns_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
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])
vpns = vpns.where("name LIKE ?", "%#{params[:q]}%") if params[:q].present?
render json: vpns, status: :ok
end

def associated_groups
vpn = Vpn.find_by_id(params[:id])
return head :not_found unless vpn.present?

groups = vpn.groups
groups = groups.where("name LIKE ?", "%#{params[:q]}%") if params[:q].present?
render json: groups, status: :ok
end

def create
if current_user.admin?
@vpn = Vpn.new(vpn_params)
Expand Down
10 changes: 7 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@
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
delete 'groups/:id/users/:user_id' => 'groups#remove_user', format: :json, constraints: { format: 'json' }
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
Expand Down

0 comments on commit 23d06c6

Please sign in to comment.