mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
admin open_api_client managing
This commit is contained in:
parent
e349adf252
commit
2c70903dfd
@ -0,0 +1,87 @@
|
||||
Application.Controllers.controller "OpenAPIClientsController", ["$scope", 'clientsPromise', 'growl', 'OpenAPIClient', 'dialogs', '_t'
|
||||
, ($scope, clientsPromise, growl, OpenAPIClient, dialogs, _t) ->
|
||||
|
||||
|
||||
|
||||
### PUBLIC SCOPE ###
|
||||
|
||||
## clients list
|
||||
$scope.clients = clientsPromise
|
||||
$scope.order = null
|
||||
$scope.clientFormVisible = false
|
||||
$scope.client = {}
|
||||
|
||||
$scope.toggleForm = ->
|
||||
$scope.clientFormVisible = !$scope.clientFormVisible
|
||||
|
||||
|
||||
# Change the order criterion to the one provided
|
||||
# @param orderBy {string} ordering criterion
|
||||
##
|
||||
$scope.setOrder = (orderBy)->
|
||||
if $scope.order == orderBy
|
||||
$scope.order = '-'+orderBy
|
||||
else
|
||||
$scope.order = orderBy
|
||||
|
||||
$scope.saveClient = (client)->
|
||||
if client.id?
|
||||
OpenAPIClient.update { id: client.id }, open_api_client: client, (clientResp)->
|
||||
client = clientResp
|
||||
growl.success(_t('client_successfully_updated'))
|
||||
else
|
||||
OpenAPIClient.save open_api_client: client, (client)->
|
||||
$scope.clients.push client
|
||||
growl.success(_t('client_successfully_created'))
|
||||
|
||||
|
||||
$scope.clientFormVisible = false
|
||||
$scope.clientForm.$setPristine()
|
||||
$scope.client = {}
|
||||
|
||||
$scope.editClient = (client)->
|
||||
$scope.clientFormVisible = true
|
||||
$scope.client = client
|
||||
|
||||
$scope.deleteClient = (index)->
|
||||
dialogs.confirm
|
||||
resolve:
|
||||
object: ->
|
||||
title: _t('confirmation_required')
|
||||
msg: _t('do_you_really_want_to_delete_this_open_api_client')
|
||||
, ->
|
||||
OpenAPIClient.delete { id: $scope.clients[index].id }, ->
|
||||
$scope.clients.splice(index, 1)
|
||||
growl.success(_t('client_successfully_deleted'))
|
||||
|
||||
$scope.resetToken = (client)->
|
||||
dialogs.confirm
|
||||
resolve:
|
||||
object: ->
|
||||
title: _t('confirmation_required')
|
||||
msg: _t('do_you_really_want_to_revoke_this_open_api_access')
|
||||
, ->
|
||||
OpenAPIClient.resetToken { id: client.id }, {}, (clientResp)->
|
||||
client.token = clientResp.token
|
||||
growl.success(_t('access_successfully_revoked'))
|
||||
|
||||
|
||||
##
|
||||
# Ask for confirmation then delete the specified administrator
|
||||
# @param admins {Array} full list of administrators
|
||||
# @param admin {Object} administrator to delete
|
||||
##
|
||||
$scope.destroyAdmin = (admins, admin)->
|
||||
dialogs.confirm
|
||||
resolve:
|
||||
object: ->
|
||||
title: _t('confirmation_required')
|
||||
msg: _t('do_you_really_want_to_delete_this_administrator_this_cannot_be_undone')
|
||||
, -> # cancel confirmed
|
||||
Admin.delete id: admin.id, ->
|
||||
admins.splice(findAdminIdxById(admins, admin.id), 1)
|
||||
growl.success(_t('administrator_successfully_deleted'))
|
||||
, (error)->
|
||||
growl.error(_t('unable_to_delete_the_administrator'))
|
||||
|
||||
]
|
@ -95,5 +95,10 @@ Application.Controllers.controller "MainNavController", ["$scope", "$location",
|
||||
linkText: 'customization'
|
||||
linkIcon: 'gear'
|
||||
}
|
||||
{
|
||||
state: 'app.admin.open_api_clients'
|
||||
linkText: 'open_api_clients'
|
||||
linkIcon: 'cloud'
|
||||
}
|
||||
]
|
||||
]
|
||||
|
@ -352,7 +352,6 @@ angular.module('application.router', ['ui.router']).
|
||||
translations: [ 'Translations', (Translations) ->
|
||||
Translations.query(['app.admin.machines_edit', 'app.shared.machine']).$promise
|
||||
]
|
||||
|
||||
# trainings
|
||||
.state 'app.logged.trainings_reserve',
|
||||
url: '/trainings/reserve'
|
||||
@ -840,5 +839,19 @@ angular.module('application.router', ['ui.router']).
|
||||
Translations.query('app.admin.settings').$promise
|
||||
]
|
||||
|
||||
# OpenAPI Clients
|
||||
.state 'app.admin.open_api_clients',
|
||||
url: '/open_api_clients'
|
||||
views:
|
||||
'main@':
|
||||
templateUrl: '<%= asset_path "admin/open_api_clients/index.html" %>'
|
||||
controller: 'OpenAPIClientsController'
|
||||
resolve:
|
||||
clientsPromise: ['OpenAPIClient', (OpenAPIClient)->
|
||||
OpenAPIClient.query().$promise
|
||||
]
|
||||
translations: [ 'Translations', (Translations) ->
|
||||
Translations.query('app.admin.open_api_clients').$promise
|
||||
]
|
||||
|
||||
]
|
||||
|
11
app/assets/javascripts/services/open_api_client.coffee
Normal file
11
app/assets/javascripts/services/open_api_client.coffee
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
Application.Services.factory 'OpenAPIClient', ["$resource", ($resource)->
|
||||
$resource "/api/open_api_clients/:id",
|
||||
{id: "@id"},
|
||||
resetToken:
|
||||
method: 'PATCH'
|
||||
url: "/api/open_api_clients/:id/reset_token"
|
||||
update:
|
||||
method: 'PUT'
|
||||
]
|
74
app/assets/templates/admin/open_api_clients/index.html.erb
Normal file
74
app/assets/templates/admin/open_api_clients/index.html.erb
Normal file
@ -0,0 +1,74 @@
|
||||
<section class="heading b-b">
|
||||
<div class="row no-gutter">
|
||||
<div class="col-xs-2 col-sm-2 col-md-1">
|
||||
<section class="heading-btn">
|
||||
<a href="#" ng-click="backPrevLocation($event)"><i class="fa fa-long-arrow-left "></i></a>
|
||||
</section>
|
||||
</div>
|
||||
<div class="col-xs-10 col-sm-10 col-md-8 b-l">
|
||||
<section class="heading-title">
|
||||
<h1 translate>{{ 'open_api_clients' }}</h1>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="m-lg">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="col-md-12">
|
||||
|
||||
<button type="button" class="btn btn-warning m-t m-b" ng-click="toggleForm()" ng-show="!clientFormVisible" translate>{{ 'add_new_client' | translate }}</button>
|
||||
|
||||
<form role="form" id="clientForm" ng-show="clientFormVisible" name="clientForm" class="form-inline m-b m-t" novalidate>
|
||||
<div class="form-group" ng-class="{'has-error': clientForm['client[name]'].$dirty && clientForm['client[name]'].$invalid}">
|
||||
<input class="form-control" type="text" name="client[name]" ng-model="client.name" value="" placeholder="{{ 'client_name' | translate }}" required>
|
||||
</div>
|
||||
|
||||
<button class="btn btn-default" ng-click="toggleForm()" name="button">{{ 'cancel' | translate }}</button>
|
||||
<input type="submit" class="btn btn-warning" ng-disabled="!client.name || client.name.length == 0" ng-click="saveClient(client)" value="{{ 'save' | translate }}">
|
||||
</form>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:20%"><a href="" ng-click="setOrder('name')">{{ 'name' | translate }} <i class="fa fa-arrows-v" ng-class="{'fa fa-sort-alpha-asc': order == 'name', 'fa fa-sort-alpha-desc': order == '-name', 'fa fa-arrows-v': order }"></i></a></th>
|
||||
|
||||
<th style="width:10%"><a href="" ng-click="setOrder('calls_count')">{{ 'calls_count' | translate }} <i class="fa fa-arrows-v" ng-class="{'fa fa-sort-numeric-asc': order == 'calls_count', 'fa fa-sort-numeric-desc': order == '-calls_count', 'fa fa-arrows-v': order }"></i></a></th>
|
||||
|
||||
<th style="width:20%"><a href="">{{ 'token' | translate }}</a></th>
|
||||
|
||||
<th style="width:20%"><a href="" ng-click="setOrder('created_at')">{{ 'created_at' | translate }} <i class="fa fa-arrows-v" ng-class="{'fa fa-sort-numeric-asc': order == 'created_at', 'fa fa-sort-numeric-desc': order == '-created_at', 'fa fa-arrows-v': order }"></i></a></th>
|
||||
|
||||
<th style="width:30%"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr ng-repeat="client in clients | orderBy: order">
|
||||
<td>{{ client.name }}</td>
|
||||
<td>{{ client.calls_count }}</td>
|
||||
<td>{{ client.token }}</td>
|
||||
<td>{{ client.created_at | amDateFormat: 'LL' }}</td>
|
||||
<td>
|
||||
<div class="buttons">
|
||||
<button class="btn btn-default" ng-click="editClient(client)">
|
||||
<i class="fa fa-pencil"></i> {{ 'edit' | translate }}
|
||||
</button>
|
||||
|
||||
<button class="btn btn-default" ng-click="resetToken(client)">
|
||||
<i class="fa fa-times"></i> {{ 'reset_token' | translate }}
|
||||
</button>
|
||||
|
||||
<button class="btn btn-default" ng-click="deleteClient($index)" ng-show="client.calls_count == 0">
|
||||
<i class="fa fa-trash"></i> {{ 'delete' | translate }}
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
@ -239,5 +239,3 @@
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
46
app/controllers/api/open_api_clients_controller.rb
Normal file
46
app/controllers/api/open_api_clients_controller.rb
Normal file
@ -0,0 +1,46 @@
|
||||
class API::OpenAPIClientsController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
authorize OpenAPI::Client
|
||||
@clients = OpenAPI::Client.order(:created_at)
|
||||
end
|
||||
# add authorization
|
||||
def create
|
||||
@client = OpenAPI::Client.new(client_params)
|
||||
authorize @client
|
||||
if @client.save
|
||||
render status: :created
|
||||
else
|
||||
render json: @client.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@client = OpenAPI::Client.find(params[:id])
|
||||
authorize @client
|
||||
if @client.update(client_params)
|
||||
render status: :ok
|
||||
else
|
||||
render json: @client.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def reset_token
|
||||
@client = OpenAPI::Client.find(params[:id])
|
||||
authorize @client
|
||||
@client.regenerate_token
|
||||
end
|
||||
|
||||
def destroy
|
||||
@client = OpenAPI::Client.find(params[:id])
|
||||
authorize @client
|
||||
@client.destroy
|
||||
head 204
|
||||
end
|
||||
|
||||
private
|
||||
def client_params
|
||||
params.require(:open_api_client).permit(:name)
|
||||
end
|
||||
end
|
21
app/policies/open_api/client_policy.rb
Normal file
21
app/policies/open_api/client_policy.rb
Normal file
@ -0,0 +1,21 @@
|
||||
class OpenAPI::ClientPolicy < ApplicationPolicy
|
||||
def index?
|
||||
user.has_role? :admin
|
||||
end
|
||||
|
||||
def create?
|
||||
user.has_role? :admin
|
||||
end
|
||||
|
||||
def update?
|
||||
user.has_role? :admin
|
||||
end
|
||||
|
||||
def reset_token?
|
||||
user.has_role? :admin
|
||||
end
|
||||
|
||||
def destroy?
|
||||
user.has_role? :admin and record.calls_count == 0
|
||||
end
|
||||
end
|
1
app/views/api/open_api_clients/_client.json.jbuilder
Normal file
1
app/views/api/open_api_clients/_client.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.extract! client, :id, :name, :calls_count, :token, :created_at
|
1
app/views/api/open_api_clients/create.json.jbuilder
Normal file
1
app/views/api/open_api_clients/create.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! 'api/open_api_clients/client', client: @client
|
3
app/views/api/open_api_clients/index.json.jbuilder
Normal file
3
app/views/api/open_api_clients/index.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
||||
json.array! @clients do |client|
|
||||
json.partial! 'api/open_api_clients/client', client: client
|
||||
end
|
1
app/views/api/open_api_clients/reset_token.json.jbuilder
Normal file
1
app/views/api/open_api_clients/reset_token.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! 'api/open_api_clients/client', client: @client
|
1
app/views/api/open_api_clients/update.json.jbuilder
Normal file
1
app/views/api/open_api_clients/update.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! 'api/open_api_clients/client', client: @client
|
@ -463,3 +463,17 @@ fr:
|
||||
reservations_cancelling: "Annulation des réservations"
|
||||
customization_of_SETTING_successfully_saved: "La personnalisation de {{SETTING}} a bien été enregistrée." # angular interpolation
|
||||
file_successfully_updated: "Le fichier a bien été mis à jour."
|
||||
|
||||
open_api_clients:
|
||||
add_new_client: "Créer un compte client"
|
||||
open_api_clients: "Clients OpenAPI"
|
||||
calls_count: "Nombre d'appels"
|
||||
created_at: "Date de création"
|
||||
reset_token: "Révoquer l'accès"
|
||||
client_name: "Nom du client"
|
||||
do_you_really_want_to_delete_this_open_api_client: "Voulez vous vraiment supprimer ce compte client OpenAPI ?"
|
||||
do_you_really_want_to_revoke_this_open_api_access: "Voulez vous vraiment revoquer l'accès de ce compte OpenAPI ? Une confirmation aura pour effet la génération d'un nouveau token."
|
||||
client_successfully_created: "Le compte client a bien été créé."
|
||||
client_successfully_updated: "Les modifications ont été enregistrées."
|
||||
client_successfully_deleted: "Le compte client a bien été supprimé."
|
||||
access_successfully_revoked: "L'accès a bien été revoqué."
|
||||
|
@ -43,6 +43,7 @@ en:
|
||||
manage_the_projects_elements: "Manage the Projects Elements"
|
||||
statistics: "Statistics"
|
||||
customization: "Customization"
|
||||
open_api_clients: "OpenAPI clients"
|
||||
|
||||
# account creation modal
|
||||
create_your_account: "Create your account"
|
||||
|
@ -43,6 +43,7 @@ fr:
|
||||
manage_the_projects_elements: "Gérer les éléments projets"
|
||||
statistics: "Statistiques"
|
||||
customization: "Personnalisation"
|
||||
open_api_clients: "Clients OpenAPI"
|
||||
|
||||
# fenêtre de création de compte
|
||||
create_your_account: "Créer votre compte"
|
||||
|
@ -93,6 +93,9 @@ Rails.application.routes.draw do
|
||||
get 'active', action: 'active', on: :collection
|
||||
end
|
||||
resources :abuses, only: [:create]
|
||||
resources :open_api_clients, only: [:index, :create, :update, :destroy] do
|
||||
patch :reset_token, on: :member
|
||||
end
|
||||
|
||||
# i18n
|
||||
get 'translations/:locale/:state' => 'translations#show', :constraints => { :state => /[^\/]+/ } # allow dots in URL for 'state'
|
||||
|
Loading…
Reference in New Issue
Block a user