2019-02-21 15:08:30 +00:00
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"encoding/json"
"net/http"
"strconv"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/db"
2022-02-22 14:01:29 +00:00
"agola.io/agola/internal/errors"
2019-07-01 09:40:20 +00:00
action "agola.io/agola/internal/services/configstore/action"
"agola.io/agola/internal/services/configstore/readdb"
"agola.io/agola/internal/util"
2019-07-31 13:39:07 +00:00
csapitypes "agola.io/agola/services/configstore/api/types"
"agola.io/agola/services/configstore/types"
2019-02-21 15:08:30 +00:00
"github.com/gorilla/mux"
2022-02-21 17:07:58 +00:00
"github.com/rs/zerolog"
2019-02-21 15:08:30 +00:00
)
2019-02-28 14:52:35 +00:00
type UserHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-02-21 15:08:30 +00:00
readDB * readdb . ReadDB
}
2022-02-21 17:07:58 +00:00
func NewUserHandler ( log zerolog . Logger , readDB * readdb . ReadDB ) * UserHandler {
return & UserHandler { log : log , readDB : readDB }
2019-02-21 15:08:30 +00:00
}
2019-02-28 14:52:35 +00:00
func ( h * UserHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
2019-07-25 08:46:02 +00:00
ctx := r . Context ( )
2019-02-21 15:08:30 +00:00
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
var user * types . User
2019-07-25 08:46:02 +00:00
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
2019-02-21 15:08:30 +00:00
var err error
2019-05-03 09:07:53 +00:00
user , err = h . readDB . GetUser ( tx , userRef )
2022-02-22 14:01:29 +00:00
return errors . WithStack ( err )
2019-02-21 15:08:30 +00:00
} )
if err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , err )
2019-02-21 15:08:30 +00:00
return
}
if user == nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrNotExist , errors . Errorf ( "user %q doesn't exist" , userRef ) ) )
2019-02-21 15:08:30 +00:00
return
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusOK , user ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
type CreateUserHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewCreateUserHandler ( log zerolog . Logger , ah * action . ActionHandler ) * CreateUserHandler {
return & CreateUserHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * CreateUserHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
2019-07-31 13:39:07 +00:00
var req * csapitypes . CreateUserRequest
2019-02-21 15:08:30 +00:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & req ) ; err != nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , err ) )
2019-02-21 15:08:30 +00:00
return
}
2019-05-03 21:35:25 +00:00
creq := & action . CreateUserRequest {
2019-03-29 16:50:51 +00:00
UserName : req . UserName ,
}
if req . CreateUserLARequest != nil {
2019-05-03 21:35:25 +00:00
creq . CreateUserLARequest = & action . CreateUserLARequest {
2019-04-29 12:57:07 +00:00
RemoteSourceName : req . CreateUserLARequest . RemoteSourceName ,
RemoteUserID : req . CreateUserLARequest . RemoteUserID ,
RemoteUserName : req . CreateUserLARequest . RemoteUserName ,
UserAccessToken : req . CreateUserLARequest . UserAccessToken ,
Oauth2AccessToken : req . CreateUserLARequest . Oauth2AccessToken ,
Oauth2RefreshToken : req . CreateUserLARequest . Oauth2RefreshToken ,
Oauth2AccessTokenExpiresAt : req . CreateUserLARequest . Oauth2AccessTokenExpiresAt ,
2019-03-29 16:50:51 +00:00
}
}
2019-05-03 21:35:25 +00:00
user , err := h . ah . CreateUser ( ctx , creq )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
return
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusCreated , user ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
2019-05-03 07:53:38 +00:00
type UpdateUserHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-05-03 07:53:38 +00:00
}
2022-02-21 17:07:58 +00:00
func NewUpdateUserHandler ( log zerolog . Logger , ah * action . ActionHandler ) * UpdateUserHandler {
return & UpdateUserHandler { log : log , ah : ah }
2019-05-03 07:53:38 +00:00
}
func ( h * UpdateUserHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-05-03 07:53:38 +00:00
2019-07-31 13:39:07 +00:00
var req * csapitypes . UpdateUserRequest
2019-05-03 07:53:38 +00:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & req ) ; err != nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , err ) )
2019-05-03 07:53:38 +00:00
return
}
2019-05-03 21:35:25 +00:00
creq := & action . UpdateUserRequest {
2019-05-03 09:07:53 +00:00
UserRef : userRef ,
2019-05-03 07:53:38 +00:00
UserName : req . UserName ,
}
2019-05-03 21:35:25 +00:00
user , err := h . ah . UpdateUser ( ctx , creq )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-05-03 07:53:38 +00:00
return
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusCreated , user ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-05-03 07:53:38 +00:00
}
}
2019-02-21 15:08:30 +00:00
type DeleteUserHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewDeleteUserHandler ( log zerolog . Logger , ah * action . ActionHandler ) * DeleteUserHandler {
return & DeleteUserHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * DeleteUserHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
2019-05-03 21:35:25 +00:00
err := h . ah . DeleteUser ( ctx , userRef )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusNoContent , nil ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-04-08 10:29:25 +00:00
}
2019-02-21 15:08:30 +00:00
}
const (
DefaultUsersLimit = 10
MaxUsersLimit = 20
)
type UsersHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-02-21 15:08:30 +00:00
readDB * readdb . ReadDB
}
2022-02-21 17:07:58 +00:00
func NewUsersHandler ( log zerolog . Logger , readDB * readdb . ReadDB ) * UsersHandler {
return & UsersHandler { log : log , readDB : readDB }
2019-02-21 15:08:30 +00:00
}
func ( h * UsersHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
2019-07-25 08:46:02 +00:00
ctx := r . Context ( )
2019-02-21 15:08:30 +00:00
query := r . URL . Query ( )
limitS := query . Get ( "limit" )
limit := DefaultUsersLimit
if limitS != "" {
var err error
limit , err = strconv . Atoi ( limitS )
if err != nil {
2022-02-22 14:01:29 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , errors . Wrapf ( err , "cannot parse limit" ) ) )
2019-02-21 15:08:30 +00:00
return
}
}
if limit < 0 {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "limit must be greater or equal than 0" ) ) )
2019-02-21 15:08:30 +00:00
return
}
if limit > MaxUsersLimit {
limit = MaxUsersLimit
}
asc := false
if _ , ok := query [ "asc" ] ; ok {
asc = true
}
start := query . Get ( "start" )
// handle special queries, like get user by token
queryType := query . Get ( "query_type" )
var users [ ] * types . User
switch queryType {
case "bytoken" :
token := query . Get ( "token" )
var user * types . User
2019-07-25 08:46:02 +00:00
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
2019-02-21 15:08:30 +00:00
var err error
user , err = h . readDB . GetUserByTokenValue ( tx , token )
2022-02-22 14:01:29 +00:00
return errors . WithStack ( err )
2019-02-21 15:08:30 +00:00
} )
if err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , err )
2019-02-21 15:08:30 +00:00
return
}
if user == nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrNotExist , errors . Errorf ( "user with required token doesn't exist" ) ) )
2019-02-21 15:08:30 +00:00
return
}
users = [ ] * types . User { user }
case "bylinkedaccount" :
linkedAccountID := query . Get ( "linkedaccountid" )
var user * types . User
2019-07-25 08:46:02 +00:00
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
2019-02-21 15:08:30 +00:00
var err error
user , err = h . readDB . GetUserByLinkedAccount ( tx , linkedAccountID )
2022-02-22 14:01:29 +00:00
return errors . WithStack ( err )
2019-02-21 15:08:30 +00:00
} )
if err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , err )
2019-02-21 15:08:30 +00:00
return
}
if user == nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrNotExist , errors . Errorf ( "user with linked account %q token doesn't exist" , linkedAccountID ) ) )
2019-02-21 15:08:30 +00:00
return
}
users = [ ] * types . User { user }
case "byremoteuser" :
remoteUserID := query . Get ( "remoteuserid" )
remoteSourceID := query . Get ( "remotesourceid" )
var user * types . User
2019-07-25 08:46:02 +00:00
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
2019-02-21 15:08:30 +00:00
var err error
user , err = h . readDB . GetUserByLinkedAccountRemoteUserIDandSource ( tx , remoteUserID , remoteSourceID )
2022-02-22 14:01:29 +00:00
return errors . WithStack ( err )
2019-02-21 15:08:30 +00:00
} )
if err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , err )
2019-02-21 15:08:30 +00:00
return
}
if user == nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrNotExist , errors . Errorf ( "user with remote user %q for remote source %q token doesn't exist" , remoteUserID , remoteSourceID ) ) )
2019-02-21 15:08:30 +00:00
return
}
users = [ ] * types . User { user }
default :
// default query
2019-07-25 08:46:02 +00:00
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
2019-02-21 15:08:30 +00:00
var err error
users , err = h . readDB . GetUsers ( tx , start , limit , asc )
2022-02-22 14:01:29 +00:00
return errors . WithStack ( err )
2019-02-21 15:08:30 +00:00
} )
if err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , err )
2019-02-21 15:08:30 +00:00
return
}
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusOK , users ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
type CreateUserLAHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewCreateUserLAHandler ( log zerolog . Logger , ah * action . ActionHandler ) * CreateUserLAHandler {
return & CreateUserLAHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * CreateUserLAHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
2019-07-31 13:39:07 +00:00
var req csapitypes . CreateUserLARequest
2019-02-21 15:08:30 +00:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & req ) ; err != nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , err ) )
2019-02-21 15:08:30 +00:00
return
}
2019-05-03 21:35:25 +00:00
creq := & action . CreateUserLARequest {
2019-05-03 09:07:53 +00:00
UserRef : userRef ,
2019-04-29 12:57:07 +00:00
RemoteSourceName : req . RemoteSourceName ,
RemoteUserID : req . RemoteUserID ,
RemoteUserName : req . RemoteUserName ,
UserAccessToken : req . UserAccessToken ,
Oauth2AccessToken : req . Oauth2AccessToken ,
Oauth2RefreshToken : req . Oauth2RefreshToken ,
Oauth2AccessTokenExpiresAt : req . Oauth2AccessTokenExpiresAt ,
2019-02-21 15:08:30 +00:00
}
2019-05-03 21:35:25 +00:00
user , err := h . ah . CreateUserLA ( ctx , creq )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
return
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusCreated , user ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
type DeleteUserLAHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewDeleteUserLAHandler ( log zerolog . Logger , ah * action . ActionHandler ) * DeleteUserLAHandler {
return & DeleteUserLAHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * DeleteUserLAHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
laID := vars [ "laid" ]
2019-05-03 21:35:25 +00:00
err := h . ah . DeleteUserLA ( ctx , userRef , laID )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusNoContent , nil ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-04-08 10:29:25 +00:00
}
2019-02-21 15:08:30 +00:00
}
type UpdateUserLAHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewUpdateUserLAHandler ( log zerolog . Logger , ah * action . ActionHandler ) * UpdateUserLAHandler {
return & UpdateUserLAHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * UpdateUserLAHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
linkedAccountID := vars [ "laid" ]
2019-07-31 13:39:07 +00:00
var req csapitypes . UpdateUserLARequest
2019-02-21 15:08:30 +00:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & req ) ; err != nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , err ) )
2019-02-21 15:08:30 +00:00
return
}
2019-05-03 21:35:25 +00:00
creq := & action . UpdateUserLARequest {
2019-05-03 09:07:53 +00:00
UserRef : userRef ,
2019-04-29 12:57:07 +00:00
LinkedAccountID : linkedAccountID ,
RemoteUserID : req . RemoteUserID ,
RemoteUserName : req . RemoteUserName ,
UserAccessToken : req . UserAccessToken ,
Oauth2AccessToken : req . Oauth2AccessToken ,
Oauth2RefreshToken : req . Oauth2RefreshToken ,
Oauth2AccessTokenExpiresAt : req . Oauth2AccessTokenExpiresAt ,
2019-02-21 15:08:30 +00:00
}
2019-05-03 21:35:25 +00:00
user , err := h . ah . UpdateUserLA ( ctx , creq )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
return
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusOK , user ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
type CreateUserTokenHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-02-21 15:08:30 +00:00
}
2022-02-21 17:07:58 +00:00
func NewCreateUserTokenHandler ( log zerolog . Logger , ah * action . ActionHandler ) * CreateUserTokenHandler {
return & CreateUserTokenHandler { log : log , ah : ah }
2019-02-21 15:08:30 +00:00
}
func ( h * CreateUserTokenHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-02-21 15:08:30 +00:00
2019-07-31 13:39:07 +00:00
var req csapitypes . CreateUserTokenRequest
2019-02-21 15:08:30 +00:00
d := json . NewDecoder ( r . Body )
if err := d . Decode ( & req ) ; err != nil {
2022-02-21 11:19:55 +00:00
util . HTTPError ( w , util . NewAPIError ( util . ErrBadRequest , err ) )
2019-02-21 15:08:30 +00:00
return
}
2019-05-03 21:35:25 +00:00
token , err := h . ah . CreateUserToken ( ctx , userRef , req . TokenName )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
return
}
2019-07-31 13:39:07 +00:00
resp := & csapitypes . CreateUserTokenResponse {
2019-02-21 15:08:30 +00:00
Token : token ,
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusCreated , resp ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-02-21 15:08:30 +00:00
}
}
2019-04-05 13:01:57 +00:00
type DeleteUserTokenHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-04-05 13:01:57 +00:00
}
2022-02-21 17:07:58 +00:00
func NewDeleteUserTokenHandler ( log zerolog . Logger , ah * action . ActionHandler ) * DeleteUserTokenHandler {
return & DeleteUserTokenHandler { log : log , ah : ah }
2019-04-05 13:01:57 +00:00
}
func ( h * DeleteUserTokenHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
2019-05-03 09:07:53 +00:00
userRef := vars [ "userref" ]
2019-04-05 13:01:57 +00:00
tokenName := vars [ "tokenname" ]
2019-05-03 21:35:25 +00:00
err := h . ah . DeleteUserToken ( ctx , userRef , tokenName )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-05-03 15:40:07 +00:00
return
2019-04-05 13:01:57 +00:00
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusNoContent , nil ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-04-08 10:29:25 +00:00
}
2019-04-05 13:01:57 +00:00
}
2019-05-03 15:40:07 +00:00
2019-07-31 13:39:07 +00:00
func userOrgsResponse ( userOrg * action . UserOrgsResponse ) * csapitypes . UserOrgsResponse {
return & csapitypes . UserOrgsResponse {
2019-05-03 15:40:07 +00:00
Organization : userOrg . Organization ,
Role : userOrg . Role ,
}
}
type UserOrgsHandler struct {
2022-02-21 17:07:58 +00:00
log zerolog . Logger
2019-05-03 21:35:25 +00:00
ah * action . ActionHandler
2019-05-03 15:40:07 +00:00
}
2022-02-21 17:07:58 +00:00
func NewUserOrgsHandler ( log zerolog . Logger , ah * action . ActionHandler ) * UserOrgsHandler {
return & UserOrgsHandler { log : log , ah : ah }
2019-05-03 15:40:07 +00:00
}
func ( h * UserOrgsHandler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) {
ctx := r . Context ( )
vars := mux . Vars ( r )
userRef := vars [ "userref" ]
2019-05-03 21:35:25 +00:00
userOrgs , err := h . ah . GetUserOrgs ( ctx , userRef )
2022-02-21 11:19:55 +00:00
if util . HTTPError ( w , err ) {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-05-03 15:40:07 +00:00
return
}
2019-07-31 13:39:07 +00:00
res := make ( [ ] * csapitypes . UserOrgsResponse , len ( userOrgs ) )
2019-05-03 15:40:07 +00:00
for i , userOrg := range userOrgs {
res [ i ] = userOrgsResponse ( userOrg )
}
2022-02-21 11:19:55 +00:00
if err := util . HTTPResponse ( w , http . StatusOK , res ) ; err != nil {
2022-02-21 17:07:58 +00:00
h . log . Err ( err ) . Send ( )
2019-05-03 15:40:07 +00:00
}
}