-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
117 lines (106 loc) · 3.08 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2019 Drone IO, Inc.
//
// 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 users
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/dchest/uniuri"
"github.com/ozonep/drone/core"
"github.com/ozonep/drone/handler/api/render"
"github.com/ozonep/drone/handler/api/request"
"github.com/ozonep/drone/logger"
)
type userWithToken struct {
*core.User
Token string `json:"token"`
}
// HandleCreate returns an http.HandlerFunc that processes an http.Request
// to create the named user account in the system.
func HandleCreate(users core.UserStore, service core.UserService, sender core.WebhookSender) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
in := new(core.User)
err := json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequest(w, err)
logger.FromRequest(r).WithError(err).
Debugln("api: cannot unmarshal request body")
return
}
user := &core.User{
Login: in.Login,
Active: true,
Admin: in.Admin,
Machine: in.Machine,
Created: time.Now().Unix(),
Updated: time.Now().Unix(),
Hash: in.Token,
}
if user.Hash == "" {
user.Hash = uniuri.NewLen(32)
}
// if the user is not a machine account, we lookup
// the user in the remote system. We can then augment
// the user input with the remote system data.
if !user.Machine {
viewer, _ := request.UserFrom(r.Context())
remote, err := service.FindLogin(r.Context(), viewer, user.Login)
if err == nil {
if user.Login != remote.Login && remote.Login != "" {
user.Login = remote.Login
}
if user.Email == "" {
user.Email = remote.Email
}
}
}
err = user.Validate()
if err != nil {
render.ErrorCode(w, err, 400)
logger.FromRequest(r).WithError(err).
Errorln("api: invlid username")
return
}
err = users.Create(r.Context(), user)
if errors.Is(err, core.ErrUserLimit) {
render.ErrorCode(w, err, 402)
logger.FromRequest(r).WithError(err).
Errorln("api: cannot create user")
return
}
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).WithError(err).
Warnln("api: cannot create user")
return
}
err = sender.Send(r.Context(), &core.WebhookData{
Event: core.WebhookEventUser,
Action: core.WebhookActionCreated,
User: user,
})
if err != nil {
logger.FromRequest(r).WithError(err).
Warnln("api: cannot send webhook")
}
var out interface{} = user
// if the user is a machine account the api token
// is included in the response.
if user.Machine {
out = &userWithToken{user, user.Hash}
}
render.JSON(w, out, 200)
}
}