feat: (rbac) update user password
This commit is contained in:
parent
236d340b3b
commit
8bcda063fb
|
@ -72,10 +72,12 @@ func (role ConsoleRole) Update(localUser *User, model rbac.Role) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
RoleMap[role.Name] = Role{
|
|
||||||
|
RoleMap[model.Name] = Role{
|
||||||
Name: model.Name,
|
Name: model.Name,
|
||||||
Platform: model.Platform,
|
Platform: model.Platform,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = orm.Save(GenerateEvent(event.ActivityMetadata{
|
err = orm.Save(GenerateEvent(event.ActivityMetadata{
|
||||||
Category: "platform",
|
Category: "platform",
|
||||||
Group: "rbac",
|
Group: "rbac",
|
||||||
|
@ -107,7 +109,7 @@ func (role ElasticsearchRole) Update(localUser *User, model rbac.Role) (err erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
RoleMap[role.Name] = Role{
|
RoleMap[model.Name] = Role{
|
||||||
Name: model.Name,
|
Name: model.Name,
|
||||||
Cluster: model.Cluster,
|
Cluster: model.Cluster,
|
||||||
ClusterPrivilege: model.ClusterPrivilege,
|
ClusterPrivilege: model.ClusterPrivilege,
|
||||||
|
@ -227,7 +229,7 @@ func (role ElasticsearchRole) Create(localUser *User) (id string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id = newRole.ID
|
id = newRole.ID
|
||||||
RoleMap[role.Name] = Role{
|
RoleMap[newRole.Name] = Role{
|
||||||
Name: newRole.Name,
|
Name: newRole.Name,
|
||||||
Cluster: newRole.Cluster,
|
Cluster: newRole.Cluster,
|
||||||
ClusterPrivilege: newRole.ClusterPrivilege,
|
ClusterPrivilege: newRole.ClusterPrivilege,
|
||||||
|
|
|
@ -21,7 +21,6 @@ func DeleteUser(localUser *User, id string) (err error) {
|
||||||
user.ID = id
|
user.ID = id
|
||||||
_, err = orm.Get(&user)
|
_, err = orm.Get(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = ErrNotFound
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = orm.Delete(user)
|
err = orm.Delete(user)
|
||||||
|
@ -55,7 +54,7 @@ func DeleteUser(localUser *User, id string) (err error) {
|
||||||
}, nil))
|
}, nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func CreateUser(localUser *User, req dto.CreateUser) (id string, err error) {
|
func CreateUser(localUser *User, req dto.CreateUser) (id string, password string, err error) {
|
||||||
q := orm.Query{Size: 1000}
|
q := orm.Query{Size: 1000}
|
||||||
q.Conds = orm.And(orm.Eq("username", req.Username))
|
q.Conds = orm.And(orm.Eq("username", req.Username))
|
||||||
|
|
||||||
|
@ -75,10 +74,9 @@ func CreateUser(localUser *User, req dto.CreateUser) (id string, err error) {
|
||||||
Name: v.Name,
|
Name: v.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
randStr := util.GenerateRandomString(8)
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
hash, err := bcrypt.GenerateFromPassword([]byte(randStr), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := rbac.User{
|
user := rbac.User{
|
||||||
|
@ -99,6 +97,7 @@ func CreateUser(localUser *User, req dto.CreateUser) (id string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id = user.ID
|
id = user.ID
|
||||||
|
password = randStr
|
||||||
err = orm.Save(GenerateEvent(event.ActivityMetadata{
|
err = orm.Save(GenerateEvent(event.ActivityMetadata{
|
||||||
Category: "platform",
|
Category: "platform",
|
||||||
Group: "rbac",
|
Group: "rbac",
|
||||||
|
@ -128,7 +127,6 @@ func UpdateUser(localUser *User, id string, req dto.UpdateUser) (err error) {
|
||||||
user.ID = id
|
user.ID = id
|
||||||
_, err = orm.Get(&user)
|
_, err = orm.Get(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = ErrNotFound
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
roles := make([]rbac.UserRole, 0)
|
roles := make([]rbac.UserRole, 0)
|
||||||
|
@ -175,7 +173,7 @@ func UpdateUserRole(localUser *User, id string, req dto.UpdateUserRole) (err err
|
||||||
user.ID = id
|
user.ID = id
|
||||||
_, err = orm.Get(&user)
|
_, err = orm.Get(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = ErrNotFound
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
changeLog, _ := util.DiffTwoObject(user, req)
|
changeLog, _ := util.DiffTwoObject(user, req)
|
||||||
|
@ -239,5 +237,38 @@ func SearchUser(keyword string, from, size int) (users orm.Result, err error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
func UpdateUserPassword(localUser *User, id string, password string) (err error) {
|
func UpdateUserPassword(localUser *User, id string, password string) (err error) {
|
||||||
|
user := rbac.User{}
|
||||||
|
user.ID = id
|
||||||
|
_, err = orm.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user.Password = string(hash)
|
||||||
|
user.Updated = time.Now()
|
||||||
|
err = orm.Save(&user)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = orm.Save(GenerateEvent(event.ActivityMetadata{
|
||||||
|
Category: "platform",
|
||||||
|
Group: "rbac",
|
||||||
|
Name: "user",
|
||||||
|
Type: "update",
|
||||||
|
Labels: util.MapStr{
|
||||||
|
"id": id,
|
||||||
|
"password": password,
|
||||||
|
"updated": user.Updated,
|
||||||
|
},
|
||||||
|
User: util.MapStr{
|
||||||
|
"userid": localUser.UserId,
|
||||||
|
"username": localUser.Username,
|
||||||
|
},
|
||||||
|
}, nil, nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,3 +38,6 @@ type UpdateUser struct {
|
||||||
type UpdateUserRole struct {
|
type UpdateUserRole struct {
|
||||||
Roles []Role `json:"roles"`
|
Roles []Role `json:"roles"`
|
||||||
}
|
}
|
||||||
|
type UpdateUserPassword struct {
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func init() {
|
||||||
api.HandleAPIMethod(api.PUT, "/user/:id", m.PermissionRequired(r.UpdateUser, enum.UserAll...))
|
api.HandleAPIMethod(api.PUT, "/user/:id", m.PermissionRequired(r.UpdateUser, enum.UserAll...))
|
||||||
api.HandleAPIMethod(api.PUT, "/user/:id/role", m.PermissionRequired(r.UpdateUserRole, enum.UserAll...))
|
api.HandleAPIMethod(api.PUT, "/user/:id/role", m.PermissionRequired(r.UpdateUserRole, enum.UserAll...))
|
||||||
api.HandleAPIMethod(api.GET, "/user/_search", m.PermissionRequired(r.SearchUser, enum.UserRead...))
|
api.HandleAPIMethod(api.GET, "/user/_search", m.PermissionRequired(r.SearchUser, enum.UserRead...))
|
||||||
|
api.HandleAPIMethod(api.PUT, "/user/:id/password", m.PermissionRequired(r.UpdateUserPassword, enum.UserAll...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadJsonConfig() {
|
func loadJsonConfig() {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"infini.sh/console/internal/core"
|
"infini.sh/console/internal/core"
|
||||||
"infini.sh/console/internal/dto"
|
"infini.sh/console/internal/dto"
|
||||||
httprouter "infini.sh/framework/core/api/router"
|
httprouter "infini.sh/framework/core/api/router"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"infini.sh/framework/modules/elastic"
|
"infini.sh/framework/modules/elastic"
|
||||||
"net/http"
|
"net/http"
|
||||||
log "src/github.com/cihub/seelog"
|
log "src/github.com/cihub/seelog"
|
||||||
|
@ -39,13 +40,17 @@ func (h Rbac) CreateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
h.Error(w, err)
|
h.Error(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id, err := biz.CreateUser(localUser, req)
|
id, pass, err := biz.CreateUser(localUser, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = log.Error(err.Error())
|
_ = log.Error(err.Error())
|
||||||
h.Error(w, err)
|
h.Error(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = h.WriteOKJSON(w, core.CreateResponse(id))
|
_ = h.WriteOKJSON(w, util.MapStr{
|
||||||
|
"_id": id,
|
||||||
|
"password": pass,
|
||||||
|
"result": "created",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -159,3 +164,29 @@ func (h Rbac) SearchUser(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func (h Rbac) UpdateUserPassword(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
id := ps.MustGetParameter("id")
|
||||||
|
var req dto.UpdateUserPassword
|
||||||
|
err := h.DecodeJSON(r, &req)
|
||||||
|
if err != nil {
|
||||||
|
_ = log.Error(err.Error())
|
||||||
|
h.Error400(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
localUser, err := biz.FromUserContext(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
h.Error(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = biz.UpdateUserPassword(localUser, id, req.Password)
|
||||||
|
if err != nil {
|
||||||
|
_ = log.Error(err.Error())
|
||||||
|
h.Error(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = h.WriteOKJSON(w, core.UpdateResponse(id))
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue