2022-01-20 13:46:51 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
2022-04-23 22:58:57 +00:00
|
|
|
"context"
|
2022-01-20 13:46:51 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-04-22 20:27:55 +00:00
|
|
|
"github.com/go-chi/render"
|
2022-01-20 13:46:51 +00:00
|
|
|
"github.com/google/uuid"
|
2022-01-23 05:58:10 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-01-20 13:46:51 +00:00
|
|
|
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
2022-04-06 00:18:26 +00:00
|
|
|
"github.com/coder/coder/coderd/gitsshkey"
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-04-29 14:04:19 +00:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-01-20 13:46:51 +00:00
|
|
|
"github.com/coder/coder/coderd/userpassword"
|
2022-03-22 19:17:50 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-01-20 13:46:51 +00:00
|
|
|
"github.com/coder/coder/cryptorand"
|
|
|
|
)
|
|
|
|
|
2022-02-10 14:33:27 +00:00
|
|
|
// Returns whether the initial user has been created or not.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) firstUser(rw http.ResponseWriter, r *http.Request) {
|
2022-02-10 14:33:27 +00:00
|
|
|
userCount, err := api.Database.GetUserCount(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user count: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-02-10 14:33:27 +00:00
|
|
|
if userCount == 0 {
|
|
|
|
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
|
|
|
Message: "The initial user has not been created!",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-02-10 14:33:27 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, httpapi.Response{
|
|
|
|
Message: "The initial user has already been created!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-20 13:46:51 +00:00
|
|
|
// Creates the initial user for a Coder deployment.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
|
2022-03-22 19:17:50 +00:00
|
|
|
var createUser codersdk.CreateFirstUserRequest
|
2022-01-20 13:46:51 +00:00
|
|
|
if !httpapi.Read(rw, r, &createUser) {
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-01-20 13:46:51 +00:00
|
|
|
// This should only function for the first user.
|
2022-02-01 22:15:26 +00:00
|
|
|
userCount, err := api.Database.GetUserCount(r.Context())
|
2022-01-20 13:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user count: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-01-20 13:46:51 +00:00
|
|
|
// If a user already exists, the initial admin user no longer can be created.
|
|
|
|
if userCount != 0 {
|
|
|
|
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
|
|
|
Message: "the initial user has already been created",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
user, organizationID, err := api.createUser(r.Context(), codersdk.CreateUserRequest{
|
|
|
|
Email: createUser.Email,
|
|
|
|
Username: createUser.Username,
|
|
|
|
Password: createUser.Password,
|
2022-01-20 13:46:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
2022-01-23 05:58:10 +00:00
|
|
|
Message: err.Error(),
|
2022-01-20 13:46:51 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-01-23 05:58:10 +00:00
|
|
|
|
2022-04-29 14:04:19 +00:00
|
|
|
// TODO: @emyrk this currently happens outside the database tx used to create
|
|
|
|
// the user. Maybe I add this ability to grant roles in the createUser api
|
|
|
|
// and add some rbac bypass when calling api functions this way??
|
|
|
|
// Add the admin role to this first user
|
|
|
|
_, err = api.Database.UpdateUserRoles(r.Context(), database.UpdateUserRolesParams{
|
|
|
|
GrantedRoles: []string{rbac.RoleAdmin(), rbac.RoleMember()},
|
|
|
|
ID: user.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusCreated, codersdk.CreateFirstUserResponse{
|
2022-03-07 17:40:54 +00:00
|
|
|
UserID: user.ID,
|
2022-04-23 22:58:57 +00:00
|
|
|
OrganizationID: organizationID,
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) users(rw http.ResponseWriter, r *http.Request) {
|
2022-04-22 20:27:55 +00:00
|
|
|
var (
|
2022-04-29 13:29:53 +00:00
|
|
|
searchName = r.URL.Query().Get("search")
|
|
|
|
statusFilter = r.URL.Query().Get("status")
|
2022-04-22 20:27:55 +00:00
|
|
|
)
|
|
|
|
|
2022-05-17 18:43:19 +00:00
|
|
|
// Reading all users across the site
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:44:09 +00:00
|
|
|
paginationParams, ok := parsePagination(rw, r)
|
|
|
|
if !ok {
|
2022-04-22 20:27:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err := api.Database.GetUsers(r.Context(), database.GetUsersParams{
|
2022-05-10 07:44:09 +00:00
|
|
|
AfterID: paginationParams.AfterID,
|
|
|
|
OffsetOpt: int32(paginationParams.Offset),
|
|
|
|
LimitOpt: int32(paginationParams.Limit),
|
2022-04-22 20:27:55 +00:00
|
|
|
Search: searchName,
|
2022-04-29 13:29:53 +00:00
|
|
|
Status: statusFilter,
|
2022-04-22 20:27:55 +00:00
|
|
|
})
|
2022-05-10 07:44:09 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
httpapi.Write(rw, http.StatusOK, []codersdk.User{})
|
|
|
|
return
|
|
|
|
}
|
2022-04-28 14:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-22 20:27:55 +00:00
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
userIDs := make([]uuid.UUID, 0, len(users))
|
|
|
|
for _, user := range users {
|
|
|
|
userIDs = append(userIDs, user.ID)
|
|
|
|
}
|
|
|
|
organizationIDsByMemberIDsRows, err := api.Database.GetOrganizationIDsByMemberIDs(r.Context(), userIDs)
|
|
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
2022-04-22 20:27:55 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-28 14:10:17 +00:00
|
|
|
organizationIDsByUserID := map[uuid.UUID][]uuid.UUID{}
|
|
|
|
for _, organizationIDsByMemberIDsRow := range organizationIDsByMemberIDsRows {
|
|
|
|
organizationIDsByUserID[organizationIDsByMemberIDsRow.UserID] = organizationIDsByMemberIDsRow.OrganizationIDs
|
|
|
|
}
|
2022-04-22 20:27:55 +00:00
|
|
|
|
|
|
|
render.Status(r, http.StatusOK)
|
2022-04-28 14:10:17 +00:00
|
|
|
render.JSON(rw, r, convertUsers(users, organizationIDsByUserID))
|
2022-04-22 20:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 19:52:58 +00:00
|
|
|
// Creates a new user.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
|
2022-05-17 18:43:19 +00:00
|
|
|
// Create the user on the site
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceUser) {
|
|
|
|
return
|
|
|
|
}
|
2022-03-07 17:40:54 +00:00
|
|
|
|
2022-03-22 19:17:50 +00:00
|
|
|
var createUser codersdk.CreateUserRequest
|
2022-01-25 19:52:58 +00:00
|
|
|
if !httpapi.Read(rw, r, &createUser) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-17 18:43:19 +00:00
|
|
|
|
|
|
|
// Create the organization member in the org.
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionCreate,
|
|
|
|
rbac.ResourceOrganizationMember.InOrg(createUser.OrganizationID)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: @emyrk Authorize the organization create if the createUser will do that.
|
|
|
|
|
2022-02-01 22:15:26 +00:00
|
|
|
_, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
|
2022-01-25 19:52:58 +00:00
|
|
|
Username: createUser.Username,
|
|
|
|
Email: createUser.Email,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
|
|
|
Message: "user already exists",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-17 18:43:19 +00:00
|
|
|
_, err = api.Database.GetOrganizationByID(r.Context(), createUser.OrganizationID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
|
|
|
Message: "organization does not exist with the provided id",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organization: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
user, _, err := api.createUser(r.Context(), createUser)
|
2022-01-25 19:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
2022-03-07 17:40:54 +00:00
|
|
|
Message: err.Error(),
|
2022-01-25 19:52:58 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
httpapi.Write(rw, http.StatusCreated, convertUser(user, []uuid.UUID{createUser.OrganizationID}))
|
2022-01-20 13:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:58:10 +00:00
|
|
|
// Returns the parameterized user requested. All validation
|
|
|
|
// is completed in the middleware for this route.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) userByName(rw http.ResponseWriter, r *http.Request) {
|
2022-01-23 05:58:10 +00:00
|
|
|
user := httpmw.UserParam(r)
|
2022-04-28 14:10:17 +00:00
|
|
|
organizationIDs, err := userOrganizationIDs(r.Context(), api, user)
|
|
|
|
|
2022-05-17 18:43:19 +00:00
|
|
|
if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUser.WithID(user.ID.String())) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-01-20 13:46:51 +00:00
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, convertUser(user, organizationIDs))
|
2022-01-20 13:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) putUserProfile(rw http.ResponseWriter, r *http.Request) {
|
2022-04-12 14:05:21 +00:00
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
|
2022-05-24 13:43:34 +00:00
|
|
|
if !api.Authorize(rw, r, rbac.ActionUpdate, rbac.ResourceUser.WithID(user.ID.String())) {
|
2022-05-17 18:43:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:05:21 +00:00
|
|
|
var params codersdk.UpdateUserProfileRequest
|
|
|
|
if !httpapi.Read(rw, r, ¶ms) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
existentUser, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
|
|
|
|
Email: params.Email,
|
|
|
|
Username: params.Username,
|
|
|
|
})
|
|
|
|
isDifferentUser := existentUser.ID != user.ID
|
|
|
|
|
|
|
|
if err == nil && isDifferentUser {
|
|
|
|
responseErrors := []httpapi.Error{}
|
|
|
|
if existentUser.Email == params.Email {
|
|
|
|
responseErrors = append(responseErrors, httpapi.Error{
|
2022-04-18 16:02:54 +00:00
|
|
|
Field: "email",
|
|
|
|
Detail: "this value is already in use and should be unique",
|
2022-04-12 14:05:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if existentUser.Username == params.Username {
|
|
|
|
responseErrors = append(responseErrors, httpapi.Error{
|
2022-04-18 16:02:54 +00:00
|
|
|
Field: "username",
|
|
|
|
Detail: "this value is already in use and should be unique",
|
2022-04-12 14:05:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
2022-05-10 21:04:23 +00:00
|
|
|
Message: "user already exists",
|
2022-04-12 14:05:21 +00:00
|
|
|
Errors: responseErrors,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) && isDifferentUser {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedUserProfile, err := api.Database.UpdateUserProfile(r.Context(), database.UpdateUserProfileParams{
|
|
|
|
ID: user.ID,
|
|
|
|
Email: params.Email,
|
|
|
|
Username: params.Username,
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("patch user: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
organizationIDs, err := userOrganizationIDs(r.Context(), api, user)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertUser(updatedUserProfile, organizationIDs))
|
2022-04-12 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseWriter, r *http.Request) {
|
2022-05-16 20:29:27 +00:00
|
|
|
return func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
apiKey := httpmw.APIKey(r)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionDelete, rbac.ResourceUser.WithID(user.ID.String())) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:29:27 +00:00
|
|
|
if status == database.UserStatusSuspended && user.ID == apiKey.UserID {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Message: "You cannot suspend yourself",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-26 14:00:07 +00:00
|
|
|
|
2022-05-16 20:29:27 +00:00
|
|
|
suspendedUser, err := api.Database.UpdateUserStatus(r.Context(), database.UpdateUserStatusParams{
|
|
|
|
ID: user.ID,
|
|
|
|
Status: status,
|
|
|
|
UpdatedAt: database.Now(),
|
2022-04-26 14:00:07 +00:00
|
|
|
})
|
|
|
|
|
2022-05-16 20:29:27 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("put user suspended: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-28 14:10:17 +00:00
|
|
|
|
2022-05-16 20:29:27 +00:00
|
|
|
organizations, err := userOrganizationIDs(r.Context(), api, user)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertUser(suspendedUser, organizations))
|
|
|
|
}
|
2022-04-26 14:00:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) {
|
2022-05-06 18:23:03 +00:00
|
|
|
var (
|
|
|
|
user = httpmw.UserParam(r)
|
2022-05-27 17:29:55 +00:00
|
|
|
apiKey = httpmw.APIKey(r)
|
2022-05-06 18:23:03 +00:00
|
|
|
params codersdk.UpdateUserPasswordRequest
|
|
|
|
)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionUpdate, rbac.ResourceUserData.WithOwner(user.ID.String())) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:20:08 +00:00
|
|
|
if !httpapi.Read(rw, r, ¶ms) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:29:55 +00:00
|
|
|
err := userpassword.Validate(params.Password)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Errors: []httpapi.Error{
|
|
|
|
{
|
|
|
|
Field: "password",
|
|
|
|
Detail: err.Error(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// we want to require old_password field if the user is changing their
|
|
|
|
// own password. This is to prevent a compromised session from being able
|
|
|
|
// to change password and lock out the user.
|
|
|
|
if user.ID == apiKey.UserID {
|
|
|
|
ok, err := userpassword.Compare(string(user.HashedPassword), params.OldPassword)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("compare user password: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Errors: []httpapi.Error{
|
|
|
|
{
|
|
|
|
Field: "old_password",
|
|
|
|
Detail: "Old password is incorrect.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:20:08 +00:00
|
|
|
hashedPassword, err := userpassword.Hash(params.Password)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("hash password: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = api.Database.UpdateUserHashedPassword(r.Context(), database.UpdateUserHashedPasswordParams{
|
|
|
|
ID: user.ID,
|
|
|
|
HashedPassword: []byte(hashedPassword),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("put user password: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusNoContent, nil)
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
|
2022-04-29 14:04:19 +00:00
|
|
|
user := httpmw.UserParam(r)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionRead, rbac.ResourceUserData.
|
|
|
|
WithOwner(user.ID.String())) {
|
|
|
|
return
|
|
|
|
}
|
2022-04-29 14:04:19 +00:00
|
|
|
|
|
|
|
resp := codersdk.UserRoles{
|
|
|
|
Roles: user.RBACRoles,
|
|
|
|
OrganizationRoles: make(map[uuid.UUID][]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships, err := api.Database.GetOrganizationMembershipsByUserID(r.Context(), user.ID)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user memberships: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:43:34 +00:00
|
|
|
// Only include ones we can read from RBAC
|
|
|
|
memberships = AuthorizeFilter(api, r, rbac.ActionRead, memberships)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
2022-05-24 13:43:34 +00:00
|
|
|
for _, mem := range memberships {
|
2022-05-17 18:43:19 +00:00
|
|
|
// If we can read the org member, include the roles
|
|
|
|
if err == nil {
|
|
|
|
resp.OrganizationRoles[mem.OrganizationID] = mem.Roles
|
|
|
|
}
|
2022-04-29 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
|
2022-04-29 14:04:19 +00:00
|
|
|
// User is the user to modify
|
|
|
|
user := httpmw.UserParam(r)
|
2022-05-17 18:43:19 +00:00
|
|
|
roles := httpmw.UserRoles(r)
|
2022-04-29 14:04:19 +00:00
|
|
|
|
|
|
|
var params codersdk.UpdateRoles
|
|
|
|
if !httpapi.Read(rw, r, ¶ms) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:00:59 +00:00
|
|
|
added, removed := rbac.ChangeRoleSet(roles.Roles, params.Roles)
|
|
|
|
for _, roleName := range added {
|
2022-05-17 18:43:19 +00:00
|
|
|
// Assigning a role requires the create permission.
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceRoleAssignment.WithID(roleName)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 16:00:59 +00:00
|
|
|
for _, roleName := range removed {
|
|
|
|
// Removing a role requires the delete permission.
|
2022-05-17 18:43:19 +00:00
|
|
|
if !api.Authorize(rw, r, rbac.ActionDelete, rbac.ResourceRoleAssignment.WithID(roleName)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 14:04:19 +00:00
|
|
|
updatedUser, err := api.updateSiteUserRoles(r.Context(), database.UpdateUserRolesParams{
|
|
|
|
GrantedRoles: params.Roles,
|
|
|
|
ID: user.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
organizationIDs, err := userOrganizationIDs(r.Context(), api, user)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertUser(updatedUser, organizationIDs))
|
|
|
|
}
|
|
|
|
|
2022-05-17 18:43:19 +00:00
|
|
|
// updateSiteUserRoles will ensure only site wide roles are passed in as arguments.
|
|
|
|
// If an organization role is included, an error is returned.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) updateSiteUserRoles(ctx context.Context, args database.UpdateUserRolesParams) (database.User, error) {
|
2022-04-29 14:04:19 +00:00
|
|
|
// Enforce only site wide roles
|
|
|
|
for _, r := range args.GrantedRoles {
|
|
|
|
if _, ok := rbac.IsOrgRole(r); ok {
|
|
|
|
return database.User{}, xerrors.Errorf("must only update site wide roles")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := rbac.RoleByName(r); err != nil {
|
|
|
|
return database.User{}, xerrors.Errorf("%q is not a supported role", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedUser, err := api.Database.UpdateUserRoles(ctx, args)
|
|
|
|
if err != nil {
|
|
|
|
return database.User{}, xerrors.Errorf("update site roles: %w", err)
|
|
|
|
}
|
|
|
|
return updatedUser, nil
|
|
|
|
}
|
|
|
|
|
2022-01-23 05:58:10 +00:00
|
|
|
// Returns organizations the parameterized user has access to.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
|
2022-01-23 05:58:10 +00:00
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
|
2022-02-01 22:15:26 +00:00
|
|
|
organizations, err := api.Database.GetOrganizationsByUserID(r.Context(), user.ID)
|
2022-02-06 00:24:51 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
organizations = []database.Organization{}
|
|
|
|
}
|
2022-01-23 05:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get organizations: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:43:34 +00:00
|
|
|
// Only return orgs the user can read
|
|
|
|
organizations = AuthorizeFilter(api, r, rbac.ActionRead, organizations)
|
|
|
|
|
2022-03-22 19:17:50 +00:00
|
|
|
publicOrganizations := make([]codersdk.Organization, 0, len(organizations))
|
2022-01-23 05:58:10 +00:00
|
|
|
for _, organization := range organizations {
|
2022-05-24 13:43:34 +00:00
|
|
|
publicOrganizations = append(publicOrganizations, convertOrganization(organization))
|
2022-01-23 05:58:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, publicOrganizations)
|
2022-01-23 05:58:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Request) {
|
2022-03-07 17:40:54 +00:00
|
|
|
organizationName := chi.URLParam(r, "organizationname")
|
|
|
|
organization, err := api.Database.GetOrganizationByName(r.Context(), organizationName)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-05-17 18:43:19 +00:00
|
|
|
// Return unauthorized rather than a 404 to not leak if the organization
|
|
|
|
// exists.
|
|
|
|
httpapi.Forbidden(rw)
|
2022-03-07 17:40:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-05-17 18:43:19 +00:00
|
|
|
httpapi.Forbidden(rw)
|
2022-03-07 17:40:54 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-17 18:43:19 +00:00
|
|
|
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionRead,
|
|
|
|
rbac.ResourceOrganization.
|
|
|
|
InOrg(organization.ID).
|
|
|
|
WithID(organization.ID.String())) {
|
2022-03-07 17:40:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, convertOrganization(organization))
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:46:51 +00:00
|
|
|
// Authenticates the user with an email and password.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
|
2022-03-22 19:17:50 +00:00
|
|
|
var loginWithPassword codersdk.LoginWithPasswordRequest
|
2022-01-20 13:46:51 +00:00
|
|
|
if !httpapi.Read(rw, r, &loginWithPassword) {
|
|
|
|
return
|
|
|
|
}
|
2022-04-28 18:22:38 +00:00
|
|
|
|
2022-02-01 22:15:26 +00:00
|
|
|
user, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
|
2022-01-20 13:46:51 +00:00
|
|
|
Email: loginWithPassword.Email,
|
|
|
|
})
|
2022-04-28 18:22:38 +00:00
|
|
|
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
|
2022-01-20 13:46:51 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get user: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-28 18:22:38 +00:00
|
|
|
|
|
|
|
// If the user doesn't exist, it will be a default struct.
|
2022-01-20 13:46:51 +00:00
|
|
|
equal, err := userpassword.Compare(string(user.HashedPassword), loginWithPassword.Password)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("compare: %s", err.Error()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !equal {
|
|
|
|
// This message is the same as above to remove ease in detecting whether
|
|
|
|
// users are registered or not. Attackers still could with a timing attack.
|
|
|
|
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
|
|
|
|
Message: "invalid email or password",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
sessionToken, created := api.createAPIKey(rw, r, database.InsertAPIKeyParams{
|
|
|
|
UserID: user.ID,
|
|
|
|
LoginType: database.LoginTypePassword,
|
2022-01-20 13:46:51 +00:00
|
|
|
})
|
2022-04-23 22:58:57 +00:00
|
|
|
if !created {
|
2022-01-20 13:46:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusCreated, codersdk.LoginWithPasswordResponse{
|
2022-01-20 13:46:51 +00:00
|
|
|
SessionToken: sessionToken,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-18 04:09:33 +00:00
|
|
|
// Creates a new session key, used for logging in via the CLI
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postAPIKey(rw http.ResponseWriter, r *http.Request) {
|
2022-02-18 04:09:33 +00:00
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
|
2022-05-17 18:43:19 +00:00
|
|
|
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceAPIKey.WithOwner(user.ID.String())) {
|
2022-02-18 04:09:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
sessionToken, created := api.createAPIKey(rw, r, database.InsertAPIKeyParams{
|
|
|
|
UserID: user.ID,
|
|
|
|
LoginType: database.LoginTypePassword,
|
2022-02-18 04:09:33 +00:00
|
|
|
})
|
2022-04-23 22:58:57 +00:00
|
|
|
if !created {
|
2022-02-18 04:09:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:58:57 +00:00
|
|
|
httpapi.Write(rw, http.StatusCreated, codersdk.GenerateAPIKeyResponse{Key: sessionToken})
|
2022-02-18 04:09:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 01:09:39 +00:00
|
|
|
// Clear the user's session cookie
|
2022-05-26 03:14:08 +00:00
|
|
|
func (*API) postLogout(rw http.ResponseWriter, _ *http.Request) {
|
2022-01-25 01:09:39 +00:00
|
|
|
// Get a blank token cookie
|
|
|
|
cookie := &http.Cookie{
|
|
|
|
// MaxAge < 0 means to delete the cookie now
|
|
|
|
MaxAge: -1,
|
2022-05-18 21:16:26 +00:00
|
|
|
Name: httpmw.SessionTokenKey,
|
2022-01-25 01:09:39 +00:00
|
|
|
Path: "/",
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(rw, cookie)
|
2022-04-12 15:17:33 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, httpapi.Response{
|
|
|
|
Message: "Logged out!",
|
|
|
|
})
|
2022-01-25 01:09:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:46:51 +00:00
|
|
|
// Generates a new ID and secret for an API key.
|
2022-01-20 16:00:13 +00:00
|
|
|
func generateAPIKeyIDSecret() (id string, secret string, err error) {
|
2022-01-20 13:46:51 +00:00
|
|
|
// Length of an API Key ID.
|
2022-01-20 16:00:13 +00:00
|
|
|
id, err = cryptorand.String(10)
|
2022-01-20 13:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
// Length of an API Key secret.
|
2022-01-20 16:00:13 +00:00
|
|
|
secret, err = cryptorand.String(22)
|
2022-01-20 13:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, secret, nil
|
|
|
|
}
|
2022-01-25 19:52:58 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params database.InsertAPIKeyParams) (string, bool) {
|
2022-04-23 22:58:57 +00:00
|
|
|
keyID, keySecret, err := generateAPIKeyIDSecret()
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("generate api key parts: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
hashed := sha256.Sum256([]byte(keySecret))
|
|
|
|
|
|
|
|
_, err = api.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
|
|
|
|
ID: keyID,
|
|
|
|
UserID: params.UserID,
|
|
|
|
ExpiresAt: database.Now().Add(24 * time.Hour),
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
HashedSecret: hashed[:],
|
|
|
|
LoginType: params.LoginType,
|
|
|
|
OAuthAccessToken: params.OAuthAccessToken,
|
|
|
|
OAuthRefreshToken: params.OAuthRefreshToken,
|
|
|
|
OAuthIDToken: params.OAuthIDToken,
|
|
|
|
OAuthExpiry: params.OAuthExpiry,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("insert api key: %s", err.Error()),
|
|
|
|
})
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// This format is consumed by the APIKey middleware.
|
|
|
|
sessionToken := fmt.Sprintf("%s-%s", keyID, keySecret)
|
|
|
|
http.SetCookie(rw, &http.Cookie{
|
2022-05-18 21:16:26 +00:00
|
|
|
Name: httpmw.SessionTokenKey,
|
2022-04-23 22:58:57 +00:00
|
|
|
Value: sessionToken,
|
|
|
|
Path: "/",
|
|
|
|
HttpOnly: true,
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
Secure: api.SecureAuthCookie,
|
|
|
|
})
|
|
|
|
return sessionToken, true
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) createUser(ctx context.Context, req codersdk.CreateUserRequest) (database.User, uuid.UUID, error) {
|
2022-04-23 22:58:57 +00:00
|
|
|
var user database.User
|
|
|
|
return user, req.OrganizationID, api.Database.InTx(func(db database.Store) error {
|
2022-04-29 14:04:19 +00:00
|
|
|
var orgRoles []string
|
2022-04-23 22:58:57 +00:00
|
|
|
// If no organization is provided, create a new one for the user.
|
|
|
|
if req.OrganizationID == uuid.Nil {
|
|
|
|
organization, err := db.InsertOrganization(ctx, database.InsertOrganizationParams{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Name: req.Username,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create organization: %w", err)
|
|
|
|
}
|
|
|
|
req.OrganizationID = organization.ID
|
2022-04-29 14:04:19 +00:00
|
|
|
orgRoles = append(orgRoles, rbac.RoleOrgAdmin(req.OrganizationID))
|
2022-04-23 22:58:57 +00:00
|
|
|
}
|
2022-04-29 14:04:19 +00:00
|
|
|
// Always also be a member
|
|
|
|
orgRoles = append(orgRoles, rbac.RoleOrgMember(req.OrganizationID))
|
2022-04-23 22:58:57 +00:00
|
|
|
|
|
|
|
params := database.InsertUserParams{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Email: req.Email,
|
|
|
|
Username: req.Username,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
2022-04-29 14:04:19 +00:00
|
|
|
// All new users are defaulted to members of the site.
|
|
|
|
RBACRoles: []string{rbac.RoleMember()},
|
2022-04-23 22:58:57 +00:00
|
|
|
}
|
|
|
|
// If a user signs up with OAuth, they can have no password!
|
|
|
|
if req.Password != "" {
|
|
|
|
hashedPassword, err := userpassword.Hash(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("hash password: %w", err)
|
|
|
|
}
|
|
|
|
params.HashedPassword = []byte(hashedPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
user, err = db.InsertUser(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, publicKey, err := gitsshkey.Generate(api.SSHKeygenAlgorithm)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("generate user gitsshkey: %w", err)
|
|
|
|
}
|
|
|
|
_, err = db.InsertGitSSHKey(ctx, database.InsertGitSSHKeyParams{
|
|
|
|
UserID: user.ID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
PublicKey: publicKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert user gitsshkey: %w", err)
|
|
|
|
}
|
|
|
|
_, err = db.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{
|
|
|
|
OrganizationID: req.OrganizationID,
|
|
|
|
UserID: user.ID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
2022-04-29 14:04:19 +00:00
|
|
|
// By default give them membership to the organization
|
|
|
|
Roles: orgRoles,
|
2022-04-23 22:58:57 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create organization member: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
func convertUser(user database.User, organizationIDs []uuid.UUID) codersdk.User {
|
2022-05-09 16:38:14 +00:00
|
|
|
convertedUser := codersdk.User{
|
2022-04-28 14:10:17 +00:00
|
|
|
ID: user.ID,
|
|
|
|
Email: user.Email,
|
|
|
|
CreatedAt: user.CreatedAt,
|
|
|
|
Username: user.Username,
|
|
|
|
Status: codersdk.UserStatus(user.Status),
|
|
|
|
OrganizationIDs: organizationIDs,
|
2022-05-09 16:38:14 +00:00
|
|
|
Roles: make([]codersdk.Role, 0),
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
2022-05-09 16:38:14 +00:00
|
|
|
|
|
|
|
for _, roleName := range user.RBACRoles {
|
|
|
|
rbacRole, _ := rbac.RoleByName(roleName)
|
|
|
|
convertedUser.Roles = append(convertedUser.Roles, convertRole(rbacRole))
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertedUser
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
2022-04-22 20:27:55 +00:00
|
|
|
|
2022-04-28 14:10:17 +00:00
|
|
|
func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][]uuid.UUID) []codersdk.User {
|
2022-04-22 20:27:55 +00:00
|
|
|
converted := make([]codersdk.User, 0, len(users))
|
|
|
|
for _, u := range users {
|
2022-04-28 14:10:17 +00:00
|
|
|
userOrganizationIDs := organizationIDsByUserID[u.ID]
|
|
|
|
converted = append(converted, convertUser(u, userOrganizationIDs))
|
2022-04-22 20:27:55 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
2022-04-28 14:10:17 +00:00
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func userOrganizationIDs(ctx context.Context, api *API, user database.User) ([]uuid.UUID, error) {
|
2022-04-28 14:10:17 +00:00
|
|
|
organizationIDsByMemberIDsRows, err := api.Database.GetOrganizationIDsByMemberIDs(ctx, []uuid.UUID{user.ID})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) || len(organizationIDsByMemberIDsRows) == 0 {
|
|
|
|
return []uuid.UUID{}, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return []uuid.UUID{}, err
|
|
|
|
}
|
|
|
|
member := organizationIDsByMemberIDsRows[0]
|
|
|
|
return member.OrganizationIDs, nil
|
|
|
|
}
|