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-04-29 12:57:07 +00:00
|
|
|
"time"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
|
|
|
action "agola.io/agola/internal/services/configstore/action"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/services/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
type UserHandler struct {
|
2019-02-21 15:08:30 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func NewUserHandler(logger *zap.Logger, readDB *readdb.ReadDB) *UserHandler {
|
|
|
|
return &UserHandler{log: logger.Sugar(), 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-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
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
2019-05-03 09:07:53 +00:00
|
|
|
user, err = h.readDB.GetUser(tx, userRef)
|
2019-02-21 15:08:30 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
httpError(w, util.NewErrNotFound(errors.Errorf("user %q doesn't exist", userRef)))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, user); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:50:51 +00:00
|
|
|
type CreateUserRequest struct {
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
|
|
|
|
CreateUserLARequest *CreateUserLARequest `json:"create_user_la_request"`
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:08:30 +00:00
|
|
|
type CreateUserHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserHandler {
|
|
|
|
return &CreateUserHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-03-29 16:50:51 +00:00
|
|
|
var req *CreateUserRequest
|
2019-02-21 15:08:30 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, user); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 07:53:38 +00:00
|
|
|
type UpdateUserRequest struct {
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateUserHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-05-03 07:53:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewUpdateUserHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateUserHandler {
|
|
|
|
return &UpdateUserHandler{log: logger.Sugar(), 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
|
|
|
|
|
|
|
var req *UpdateUserRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
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)
|
2019-05-03 07:53:38 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, user); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:08:30 +00:00
|
|
|
type DeleteUserHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteUserHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserHandler {
|
|
|
|
return &DeleteUserHandler{log: logger.Sugar(), 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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultUsersLimit = 10
|
|
|
|
MaxUsersLimit = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsersHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUsersHandler(logger *zap.Logger, readDB *readdb.ReadDB) *UsersHandler {
|
|
|
|
return &UsersHandler{log: logger.Sugar(), readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultUsersLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("cannot parse limit: %w", err)))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
user, err = h.readDB.GetUserByTokenValue(tx, token)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if user == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(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
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
user, err = h.readDB.GetUserByLinkedAccount(tx, linkedAccountID)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if user == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(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
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
user, err = h.readDB.GetUserByLinkedAccountRemoteUserIDandSource(tx, remoteUserID, remoteSourceID)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if user == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(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
|
|
|
|
err := h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
users, err = h.readDB.GetUsers(tx, start, limit, asc)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-05 14:33:00 +00:00
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, users); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLARequest struct {
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName string `json:"remote_source_name"`
|
|
|
|
RemoteUserID string `json:"remote_user_id"`
|
|
|
|
RemoteUserName string `json:"remote_user_name"`
|
|
|
|
UserAccessToken string `json:"user_access_token"`
|
|
|
|
Oauth2AccessToken string `json:"oauth2_access_token"`
|
|
|
|
Oauth2RefreshToken string `json:"oauth2_refresh_token"`
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time `json:"oauth_2_access_token_expires_at"`
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLAHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserLAHandler {
|
|
|
|
return &CreateUserLAHandler{log: logger.Sugar(), 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
|
|
|
|
|
|
|
var req CreateUserLARequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, user); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteUserLAHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserLAHandler {
|
|
|
|
return &DeleteUserLAHandler{log: logger.Sugar(), 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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateUserLARequest struct {
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteUserID string `json:"remote_user_id"`
|
|
|
|
RemoteUserName string `json:"remote_user_name"`
|
|
|
|
UserAccessToken string `json:"user_access_token"`
|
|
|
|
Oauth2AccessToken string `json:"oauth2_access_token"`
|
|
|
|
Oauth2RefreshToken string `json:"oauth2_refresh_token"`
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time `json:"oauth_2_access_token_expires_at"`
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateUserLAHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewUpdateUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateUserLAHandler {
|
|
|
|
return &UpdateUserLAHandler{log: logger.Sugar(), 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"]
|
|
|
|
|
|
|
|
var req UpdateUserLARequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, user); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenRequest struct {
|
|
|
|
TokenName string `json:"token_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenResponse struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserTokenHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserTokenHandler {
|
|
|
|
return &CreateUserTokenHandler{log: logger.Sugar(), 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
|
|
|
|
|
|
|
var req CreateUserTokenRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(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)
|
2019-03-12 14:12:19 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &CreateUserTokenResponse{
|
|
|
|
Token: token,
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, resp); err != nil {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 13:01:57 +00:00
|
|
|
|
|
|
|
type DeleteUserTokenHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserTokenHandler {
|
|
|
|
return &DeleteUserTokenHandler{log: logger.Sugar(), 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)
|
2019-04-05 13:01:57 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-05-03 15:40:07 +00:00
|
|
|
return
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-05 13:01:57 +00:00
|
|
|
}
|
2019-05-03 15:40:07 +00:00
|
|
|
|
|
|
|
type UserOrgsResponse struct {
|
|
|
|
Organization *types.Organization
|
|
|
|
Role types.MemberRole
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func userOrgsResponse(userOrg *action.UserOrgsResponse) *UserOrgsResponse {
|
2019-05-03 15:40:07 +00:00
|
|
|
return &UserOrgsResponse{
|
|
|
|
Organization: userOrg.Organization,
|
|
|
|
Role: userOrg.Role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserOrgsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-05-03 15:40:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewUserOrgsHandler(logger *zap.Logger, ah *action.ActionHandler) *UserOrgsHandler {
|
|
|
|
return &UserOrgsHandler{log: logger.Sugar(), 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)
|
2019-05-03 15:40:07 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*UserOrgsResponse, len(userOrgs))
|
|
|
|
for i, userOrg := range userOrgs {
|
|
|
|
res[i] = userOrgsResponse(userOrg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|