diff --git a/admin/auth.go b/admin/auth.go index a12f6c78..561a5fcf 100644 --- a/admin/auth.go +++ b/admin/auth.go @@ -154,7 +154,7 @@ func handlerAuthCheck(h http.Handler) http.Handler { http.Redirect(w, r, forbiddenPath, http.StatusForbidden) return } - newUser, err := adminUsers.New(username, "", email, fullname, (s[ctxLevel] == adminLevel)) + newUser, err := adminUsers.New(username, "", email, fullname, headersConfig.DefaultEnv, (s[ctxLevel] == adminLevel)) if err != nil { log.Printf("Error with new user %s: %v", username, err) http.Redirect(w, r, forbiddenPath, http.StatusFound) diff --git a/admin/handlers/post.go b/admin/handlers/post.go index 56685597..ef3f5c4c 100644 --- a/admin/handlers/post.go +++ b/admin/handlers/post.go @@ -40,7 +40,9 @@ func (h *HandlersAdmin) LoginPOSTHandler(w http.ResponseWriter, r *http.Request) } permissions, err := h.Users.ConvertPermissions(user.Permissions.RawMessage) if err != nil { - + adminErrorResponse(w, "error processing login", http.StatusInternalServerError, err) + h.Inc(metricAdminErr) + return } _, err = h.Sessions.Save(r, w, user, permissions) if err != nil { @@ -52,7 +54,7 @@ func (h *HandlersAdmin) LoginPOSTHandler(w http.ResponseWriter, r *http.Request) if h.Settings.DebugService(settings.ServiceAdmin) { log.Println("DebugService: Login response sent") } - adminOKResponse(w, "OK") + adminOKResponse(w, "/environment/"+user.DefaultEnv+"/active") h.Inc(metricAdminOK) } @@ -987,7 +989,7 @@ func (h *HandlersAdmin) EnvsPOSTHandler(w http.ResponseWriter, r *http.Request) adminOKResponse(w, "environment created successfully") case "delete": if c.Name == h.Settings.DefaultEnv(settings.ServiceAdmin) { - adminErrorResponse(w, "not a good idea", http.StatusInternalServerError, fmt.Errorf("attempt to remove environment %s", c.Name)) + adminErrorResponse(w, "nope, this is the default environment", http.StatusInternalServerError, fmt.Errorf("attempt to remove default environment %s", c.Name)) h.Inc(metricAdminErr) return } @@ -1153,8 +1155,14 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request) h.Inc(metricAdminErr) return } + // Check that default environment exists + if (u.DefaultEnv == "") || !h.Envs.Exists(u.DefaultEnv) { + adminErrorResponse(w, "error adding user", http.StatusInternalServerError, fmt.Errorf("environment %s does not exist", u.DefaultEnv)) + h.Inc(metricAdminErr) + return + } // Prepare user to create - newUser, err := h.Users.New(u.Username, u.Password, u.Email, u.Fullname, u.Admin) + newUser, err := h.Users.New(u.Username, u.Password, u.Email, u.Fullname, u.DefaultEnv, u.Admin) if err != nil { adminErrorResponse(w, "error with new user", http.StatusInternalServerError, err) h.Inc(metricAdminErr) @@ -1166,19 +1174,22 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request) h.Inc(metricAdminErr) return } + namesEnvs := []string{u.DefaultEnv} + access := users.EnvLevel if u.Admin { - namesEnvs, err := h.Envs.Names() + access = users.AdminLevel + namesEnvs, err = h.Envs.Names() if err != nil { adminErrorResponse(w, "error getting environments user", http.StatusInternalServerError, err) h.Inc(metricAdminErr) return } - perms := h.Users.GenPermissions(namesEnvs, u.Admin) - if err := h.Users.ChangePermissions(u.Username, perms); err != nil { - adminErrorResponse(w, "error changing permissions", http.StatusInternalServerError, err) - h.Inc(metricAdminErr) - return - } + } + perms := h.Users.GenPermissions(namesEnvs, access) + if err := h.Users.ChangePermissions(u.Username, perms); err != nil { + adminErrorResponse(w, "error changing permissions", http.StatusInternalServerError, err) + h.Inc(metricAdminErr) + return } if u.Token { token, exp, err := h.Users.CreateToken(newUser.Username) @@ -1209,6 +1220,13 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request) return } } + if u.DefaultEnv != "" { + if err := h.Users.ChangeDefaultEnv(u.Username, u.DefaultEnv); err != nil { + adminErrorResponse(w, "error changing default environment", http.StatusInternalServerError, err) + h.Inc(metricAdminErr) + return + } + } adminOKResponse(w, "user updated successfully") case "remove": if u.Username == ctx[sessions.CtxUser] { @@ -1243,7 +1261,7 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request) h.Inc(metricAdminErr) return } - perms := h.Users.GenPermissions(namesEnvs, u.Admin) + perms := h.Users.GenPermissions(namesEnvs, users.AdminLevel) if err := h.Users.ChangePermissions(u.Username, perms); err != nil { adminErrorResponse(w, "error changing permissions", http.StatusInternalServerError, err) h.Inc(metricAdminErr) diff --git a/admin/handlers/types-requests.go b/admin/handlers/types-requests.go index d68c27f1..9e111862 100644 --- a/admin/handlers/types-requests.go +++ b/admin/handlers/types-requests.go @@ -107,14 +107,15 @@ type EnvironmentsRequest struct { // UsersRequest to receive user action requests type UsersRequest struct { - CSRFToken string `json:"csrftoken"` - Action string `json:"action"` - Username string `json:"username"` - Email string `json:"email"` - Fullname string `json:"fullname"` - Password string `json:"password"` - Token bool `json:"token"` - Admin bool `json:"admin"` + CSRFToken string `json:"csrftoken"` + Action string `json:"action"` + Username string `json:"username"` + Email string `json:"email"` + Fullname string `json:"fullname"` + Password string `json:"password"` + Token bool `json:"token"` + Admin bool `json:"admin"` + DefaultEnv string `json:"environment"` } // TagsRequest to receive tag action requests diff --git a/admin/static/js/login.js b/admin/static/js/login.js index d04e6cad..819452d9 100644 --- a/admin/static/js/login.js +++ b/admin/static/js/login.js @@ -7,7 +7,9 @@ function sendLogin() { username: _user, password: _password }; - sendPostRequest(data, _url, '/dashboard', false); + sendPostRequest(data, _url, '', false, function(_data){ + window.location.replace(_data.message); + }); } function sendLogout() { diff --git a/admin/static/js/users.js b/admin/static/js/users.js index c3e68ee0..f9784bac 100644 --- a/admin/static/js/users.js +++ b/admin/static/js/users.js @@ -17,6 +17,7 @@ function confirmAddUser() { var _password = $("#user_password").val(); var _admin = $("#user_admin").is(':checked'); var _token = $("#user_token").is(':checked'); + var _env = $("#default_env").val(); var data = { csrftoken: _csrftoken, @@ -26,7 +27,8 @@ function confirmAddUser() { fullname: _fullname, password: _password, admin: _admin, - token: _token + token: _token, + environment: _env }; sendPostRequest(data, _url, _url, false); } diff --git a/admin/templates/users.html b/admin/templates/users.html index c80e2a1f..dc735552 100644 --- a/admin/templates/users.html +++ b/admin/templates/users.html @@ -125,7 +125,6 @@