support get users by org (#426)

* support get users by org
This commit is contained in:
qinyening 2020-11-28 11:42:46 +08:00 committed by GitHub
parent 5d0c6c0c6e
commit 0b696202e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 24 deletions

View File

@ -277,7 +277,7 @@ func (u *User) Save() error {
return err
}
func UserTotal(ids []int64, query string) (int64, error) {
func UserTotal(ids []int64, where string, args ...interface{}) (int64, error) {
session := DB["rdb"].NewSession()
defer session.Close()
@ -285,23 +285,21 @@ func UserTotal(ids []int64, query string) (int64, error) {
session = session.In("id", ids)
}
if query != "" {
q := "%" + query + "%"
return session.Where("username like ? or dispname like ? or phone like ? or email like ?", q, q, q, q).Count(new(User))
if where != "" {
session = session.Where(where, args...)
}
return session.Count(new(User))
}
func UserGets(ids []int64, query string, limit, offset int) ([]User, error) {
func UserGets(ids []int64, limit, offset int, where string, args ...interface{}) ([]User, error) {
session := DB["rdb"].Limit(limit, offset).OrderBy("username")
if len(ids) > 0 {
session = session.In("id", ids)
}
if query != "" {
q := "%" + query + "%"
session = session.Where("username like ? or dispname like ? or phone like ? or email like ?", q, q, q, q)
if where != "" {
session = session.Where(where, args...)
}
var users []User

View File

@ -140,3 +140,30 @@ func UsernameCandoNodeOp(username, operation string, nodeId int64) (bool, error)
return user.HasPermByNode(node, operation)
}
func UserAndTotalGets(query, org string, limit, offset int, ids []int64) ([]User, int64, error) {
where := "1 = 1"
param := []interface{}{}
if query != "" {
q := "%" + query + "%"
where += " and (username like ? or dispname like ? or phone like ? or email like ?)"
param = append(param, q, q, q, q)
}
if org != "" {
q := "%" + org + "%"
where += " and organization like ?"
param = append(param, q)
}
total, err := UserTotal(ids, where, param...)
if err != nil {
return []User{}, total, err
}
list, err := UserGets(ids, limit, offset, where, param...)
return list, total, err
}

View File

@ -174,7 +174,7 @@ func Config(r *gin.Engine) {
v1.GET("/get-teams-by-ids", v1TeamGetByIds)
v1.GET("/get-user-ids-by-team-ids", v1UserIdsGetByTeamIds)
v1.GET("/users", userListGet)
v1.GET("/users", v1UserListGet)
v1.POST("/login", v1Login)
v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms)

View File

@ -15,12 +15,10 @@ import (
func userListGet(c *gin.Context) {
limit := queryInt(c, "limit", 20)
query := queryStr(c, "query", "")
org := queryStr(c, "org", "")
ids := str.IdsInt64(queryStr(c, "ids", ""))
total, err := models.UserTotal(ids, query)
dangerous(err)
list, err := models.UserGets(ids, query, limit, offset(c, limit))
list, total, err := models.UserAndTotalGets(query, org, limit, offset(c, limit), ids)
dangerous(err)
for i := 0; i < len(list); i++ {
@ -33,6 +31,20 @@ func userListGet(c *gin.Context) {
}, nil)
}
func v1UserListGet(c *gin.Context) {
limit := queryInt(c, "limit", 20)
query := queryStr(c, "query", "")
org := queryStr(c, "org", "")
ids := str.IdsInt64(queryStr(c, "ids", ""))
list, total, err := models.UserAndTotalGets(query, org, limit, offset(c, limit), ids)
renderData(c, gin.H{
"list": list,
"total": total,
}, err)
}
type userProfileForm struct {
Username string `json:"username"`
Password string `json:"password"`
@ -44,6 +56,7 @@ type userProfileForm struct {
LeaderId int64 `json:"leader_id"`
Typ int `json:"typ"`
Status int `json:"status"`
Organization string `json:"organization"`
}
func userAddPost(c *gin.Context) {
@ -141,7 +154,17 @@ func userProfilePut(c *gin.Context) {
target.Status = f.Status
}
err := target.Update("dispname", "phone", "email", "im", "is_root", "leader_id", "leader_name", "typ", "status")
if f.Status != target.Status {
arr = append(arr, fmt.Sprintf("typ: %s -> %s", target.Status, f.Status))
target.Status = f.Status
}
if f.Organization != target.Organization {
arr = append(arr, fmt.Sprintf("organization: %s -> %s", target.Organization, f.Organization))
target.Organization = f.Organization
}
err := target.Update("dispname", "phone", "email", "im", "is_root", "leader_id", "leader_name", "typ", "status", "organization")
if err == nil && len(arr) > 0 {
content := strings.Join(arr, "")
go models.OperationLogNew(root.Username, "user", target.Id, fmt.Sprintf("UserModify %s %s", target.Username, content))