Semaphore/api/users.go

103 lines
2.3 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package api
import (
"database/sql"
"net/http"
2016-04-29 09:33:08 +02:00
"time"
2017-02-23 06:12:16 +01:00
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/util"
"github.com/castawaylabs/mulekick"
"github.com/gorilla/context"
"golang.org/x/crypto/bcrypt"
)
2017-02-22 23:17:36 +01:00
func getUsers(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
var users []db.User
if _, err := db.Mysql.Select(&users, "select * from user"); err != nil {
panic(err)
}
mulekick.WriteJSON(w, http.StatusOK, users)
}
2017-02-22 23:17:36 +01:00
func addUser(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
var user db.User
if err := mulekick.Bind(w, r, &user); err != nil {
return
}
2016-04-29 09:33:08 +02:00
user.Created = time.Now()
2017-02-23 06:12:16 +01:00
if err := db.Mysql.Insert(&user); err != nil {
panic(err)
}
mulekick.WriteJSON(w, http.StatusCreated, user)
}
2017-02-22 23:17:36 +01:00
func getUserMiddleware(w http.ResponseWriter, r *http.Request) {
2017-02-23 00:21:49 +01:00
userID, err := util.GetIntParam("user_id", w, r)
if err != nil {
return
}
2017-02-23 06:12:16 +01:00
var user db.User
if err := db.Mysql.SelectOne(&user, "select * from user where id=?", userID); err != nil {
if err == sql.ErrNoRows {
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNotFound)
return
}
panic(err)
}
context.Set(r, "_user", user)
}
2017-02-22 23:17:36 +01:00
func updateUser(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
oldUser := context.Get(r, "_user").(db.User)
2017-02-23 06:12:16 +01:00
var user db.User
if err := mulekick.Bind(w, r, &user); err != nil {
return
}
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("update user set name=?, username=?, email=? where id=?", user.Name, user.Username, user.Email, oldUser.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}
2017-02-22 23:17:36 +01:00
func updateUserPassword(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
user := context.Get(r, "_user").(db.User)
var pwd struct {
Pwd string `json:"password"`
}
if err := mulekick.Bind(w, r, &pwd); err != nil {
return
}
password, _ := bcrypt.GenerateFromPassword([]byte(pwd.Pwd), 11)
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("update user set password=? where id=?", string(password), user.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}
2017-02-22 23:17:36 +01:00
func deleteUser(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
user := context.Get(r, "_user").(db.User)
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("delete from project__user where user_id=?", user.ID); err != nil {
panic(err)
}
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("delete from user where id=?", user.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}