2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2020-10-20 23:14:03 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-05-09 19:54:20 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-10-20 23:14:03 +08:00
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/utils/pagination"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2020-10-20 23:14:03 +08:00
|
|
|
)
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetGlobalUsers
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetGlobalUsers
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get global users
|
|
|
|
// @Success 200 {array} object.User The Response object
|
|
|
|
// @router /get-global-users [get]
|
2021-02-13 00:11:12 +08:00
|
|
|
func (c *ApiController) GetGlobalUsers() {
|
2021-11-06 11:32:22 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
2021-12-25 10:55:10 +08:00
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
maskedUsers, err := object.GetMaskedUsers(object.GetGlobalUsers())
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(maskedUsers)
|
2021-11-06 11:32:22 +08:00
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-05-30 15:49:39 +08:00
|
|
|
count, err := object.GetGlobalUserCount(field, value)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
|
|
|
users, err := object.GetPaginationGlobalUsers(paginator.Offset(), limit, field, value, sortField, sortOrder)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err = object.GetMaskedUsers(users)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ResponseOk(users, paginator.Nums())
|
|
|
|
}
|
2021-02-13 00:11:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetUsers
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetUsers
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description
|
|
|
|
// @Param owner query string true "The owner of users"
|
|
|
|
// @Success 200 {array} object.User The Response object
|
|
|
|
// @router /get-users [get]
|
2020-10-20 23:14:03 +08:00
|
|
|
func (c *ApiController) GetUsers() {
|
|
|
|
owner := c.Input().Get("owner")
|
2023-06-18 23:33:13 +08:00
|
|
|
groupName := c.Input().Get("groupName")
|
2021-11-06 11:32:22 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
2021-12-25 10:55:10 +08:00
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-06-18 23:33:13 +08:00
|
|
|
if groupName != "" {
|
2023-08-11 10:59:18 +08:00
|
|
|
maskedUsers, err := object.GetMaskedUsers(object.GetGroupUsers(util.GetId(owner, groupName)))
|
2023-06-12 09:27:16 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.ResponseOk(maskedUsers)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
maskedUsers, err := object.GetMaskedUsers(object.GetUsers(owner))
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(maskedUsers)
|
2021-11-06 11:32:22 +08:00
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-06-18 23:33:13 +08:00
|
|
|
count, err := object.GetUserCount(owner, field, value, groupName)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
2023-06-18 23:33:13 +08:00
|
|
|
users, err := object.GetPaginationUsers(owner, paginator.Offset(), limit, field, value, sortField, sortOrder, groupName)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err = object.GetMaskedUsers(users)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ResponseOk(users, paginator.Nums())
|
|
|
|
}
|
2020-10-20 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetUser
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetUser
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get user
|
2023-05-19 20:46:44 +07:00
|
|
|
// @Param id query string false "The id ( owner/name ) of the user"
|
2022-07-31 01:02:28 +08:00
|
|
|
// @Param owner query string false "The owner of the user"
|
|
|
|
// @Param email query string false "The email of the user"
|
|
|
|
// @Param phone query string false "The phone of the user"
|
2023-03-30 18:39:14 +08:00
|
|
|
// @Param userId query string false "The userId of the user"
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Success 200 {object} object.User The Response object
|
|
|
|
// @router /get-user [get]
|
2020-10-20 23:14:03 +08:00
|
|
|
func (c *ApiController) GetUser() {
|
|
|
|
id := c.Input().Get("id")
|
2021-11-19 10:43:14 +08:00
|
|
|
email := c.Input().Get("email")
|
2022-07-31 01:02:28 +08:00
|
|
|
phone := c.Input().Get("phone")
|
2022-07-14 21:46:13 +08:00
|
|
|
userId := c.Input().Get("userId")
|
2022-07-13 22:56:35 +08:00
|
|
|
owner := c.Input().Get("owner")
|
2023-05-30 15:49:39 +08:00
|
|
|
var err error
|
2023-05-19 20:46:44 +07:00
|
|
|
var userFromUserId *object.User
|
|
|
|
if userId != "" && owner != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
userFromUserId, err = object.GetUserByUserId(owner, userId)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:46:44 +07:00
|
|
|
id = util.GetId(userFromUserId.Owner, userFromUserId.Name)
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:48:55 -03:00
|
|
|
var user *object.User
|
2022-07-13 22:56:35 +08:00
|
|
|
|
2023-10-12 15:48:55 -03:00
|
|
|
if id == "" && owner == "" {
|
|
|
|
switch {
|
|
|
|
case email != "":
|
|
|
|
user, err = object.GetUserByEmailOnly(email)
|
|
|
|
case phone != "":
|
|
|
|
user, err = object.GetUserByPhoneOnly(phone)
|
|
|
|
case userId != "":
|
|
|
|
user, err = object.GetUserByUserIdOnly(userId)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if owner == "" {
|
|
|
|
owner = util.GetOwnerFromId(id)
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2023-10-12 15:48:55 -03:00
|
|
|
organization, err := object.GetOrganization(util.GetId("admin", owner))
|
|
|
|
if err != nil {
|
2022-04-16 15:10:03 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-11-19 10:43:14 +08:00
|
|
|
|
2023-10-12 15:48:55 -03:00
|
|
|
if !organization.IsProfilePublic {
|
|
|
|
requestUserId := c.GetSessionUsername()
|
|
|
|
hasPermission, err := object.CheckUserPermission(requestUserId, id, false, c.GetAcceptLanguage())
|
|
|
|
if !hasPermission {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case email != "":
|
|
|
|
user, err = object.GetUserByEmail(owner, email)
|
|
|
|
case phone != "":
|
|
|
|
user, err = object.GetUserByPhone(owner, phone)
|
|
|
|
case userId != "":
|
|
|
|
user = userFromUserId
|
|
|
|
default:
|
|
|
|
user, err = object.GetUser(id)
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-11-19 10:43:14 +08:00
|
|
|
}
|
2020-10-20 23:14:03 +08:00
|
|
|
|
2023-07-14 20:57:59 +08:00
|
|
|
if user != nil {
|
|
|
|
user.MultiFactorAuths = object.GetAllMfaProps(user, true)
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.ExtendUserWithRolesAndPermissions(user)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
2022-07-30 17:31:56 +08:00
|
|
|
|
2023-07-19 19:14:53 +08:00
|
|
|
isAdminOrSelf := c.IsAdminOrSelf(user)
|
|
|
|
maskedUser, err := object.GetMaskedUser(user, isAdminOrSelf)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(maskedUser)
|
2020-10-20 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// UpdateUser
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title UpdateUser
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description update user
|
2023-02-10 10:42:16 +08:00
|
|
|
// @Param id query string true "The id ( owner/name ) of the user"
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Param body body object.User true "The details of the user"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /update-user [post]
|
2020-10-20 23:14:03 +08:00
|
|
|
func (c *ApiController) UpdateUser() {
|
|
|
|
id := c.Input().Get("id")
|
2021-12-11 14:45:08 +08:00
|
|
|
columnsStr := c.Input().Get("columns")
|
2020-10-20 23:14:03 +08:00
|
|
|
|
|
|
|
var user object.User
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-10-20 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2023-04-09 10:35:30 +08:00
|
|
|
if id == "" {
|
|
|
|
id = c.GetSessionUsername()
|
|
|
|
if id == "" {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
oldUser, err := object.GetUser(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:35:30 +08:00
|
|
|
if oldUser == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), id))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 22:07:19 +08:00
|
|
|
if oldUser.Owner == "built-in" && oldUser.Name == "admin" && (user.Owner != "built-in" || user.Name != "admin") {
|
|
|
|
c.ResponseError(c.T("auth:Unauthorized operation"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-02 11:59:07 +08:00
|
|
|
if c.Input().Get("allowEmpty") == "" {
|
|
|
|
if user.DisplayName == "" {
|
|
|
|
c.ResponseError(c.T("user:Display name cannot be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:35:30 +08:00
|
|
|
if msg := object.CheckUpdateUser(oldUser, &user, c.GetAcceptLanguage()); msg != "" {
|
2023-01-10 22:34:08 +08:00
|
|
|
c.ResponseError(msg)
|
2021-05-16 21:52:50 +08:00
|
|
|
return
|
|
|
|
}
|
2023-04-25 23:05:53 +08:00
|
|
|
|
|
|
|
isAdmin := c.IsAdmin()
|
|
|
|
if pass, err := object.CheckPermissionForUpdateUser(oldUser, &user, isAdmin, c.GetAcceptLanguage()); !pass {
|
2023-04-09 10:35:30 +08:00
|
|
|
c.ResponseError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-16 21:52:50 +08:00
|
|
|
|
2021-12-11 14:45:08 +08:00
|
|
|
columns := []string{}
|
|
|
|
if columnsStr != "" {
|
|
|
|
columns = strings.Split(columnsStr, ",")
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
affected, err := object.UpdateUser(id, &user, columns, isAdmin)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:27:26 +08:00
|
|
|
if affected {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.UpdateUserToOriginalDatabase(&user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-06-20 13:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
2020-10-20 23:14:03 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// AddUser
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title AddUser
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description add user
|
|
|
|
// @Param body body object.User true "The details of the user"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /add-user [post]
|
2020-10-20 23:14:03 +08:00
|
|
|
func (c *ApiController) AddUser() {
|
|
|
|
var user object.User
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-10-20 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2023-06-12 09:27:16 +08:00
|
|
|
count, err := object.GetUserCount("", "", "", "")
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkQuotaForUser(int(count)); err != nil {
|
2022-11-29 11:01:41 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
msg := object.CheckUsername(user.Name, c.GetAcceptLanguage())
|
2022-10-10 19:58:02 +08:00
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.AddUser(&user))
|
2020-10-20 23:14:03 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// DeleteUser
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title DeleteUser
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description delete user
|
|
|
|
// @Param body body object.User true "The details of the user"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /delete-user [post]
|
2020-10-20 23:14:03 +08:00
|
|
|
func (c *ApiController) DeleteUser() {
|
|
|
|
var user object.User
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-10-20 23:14:03 +08:00
|
|
|
}
|
|
|
|
|
2023-05-04 22:07:19 +08:00
|
|
|
if user.Owner == "built-in" && user.Name == "admin" {
|
|
|
|
c.ResponseError(c.T("auth:Unauthorized operation"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:48:34 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteUser(&user))
|
2020-10-20 23:14:03 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
2021-05-09 19:54:20 +08:00
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetEmailAndPhone
|
2021-06-02 13:39:01 +08:00
|
|
|
// @Title GetEmailAndPhone
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-06-02 13:39:01 +08:00
|
|
|
// @Description get email and phone by username
|
|
|
|
// @Param username formData string true "The username of the user"
|
|
|
|
// @Param organization formData string true "The organization of the user"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
2023-03-01 15:57:42 +08:00
|
|
|
// @router /get-email-and-phone [get]
|
2021-06-02 13:39:01 +08:00
|
|
|
func (c *ApiController) GetEmailAndPhone() {
|
2023-03-01 15:57:42 +08:00
|
|
|
organization := c.Ctx.Request.Form.Get("organization")
|
|
|
|
username := c.Ctx.Request.Form.Get("username")
|
2021-06-02 13:39:01 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err := object.GetUserByFields(organization, username)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:39:01 +08:00
|
|
|
if user == nil {
|
2023-03-01 15:57:42 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), util.GetId(organization, username)))
|
2021-06-02 13:39:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:54:57 +08:00
|
|
|
respUser := object.User{Name: user.Name}
|
2021-06-07 19:12:51 +08:00
|
|
|
var contentType string
|
2023-03-01 15:57:42 +08:00
|
|
|
switch username {
|
2021-06-07 19:12:51 +08:00
|
|
|
case user.Email:
|
|
|
|
contentType = "email"
|
2022-03-15 12:54:57 +08:00
|
|
|
respUser.Email = user.Email
|
2021-06-07 19:12:51 +08:00
|
|
|
case user.Phone:
|
|
|
|
contentType = "phone"
|
2022-03-15 12:54:57 +08:00
|
|
|
respUser.Phone = user.Phone
|
2021-06-07 19:12:51 +08:00
|
|
|
case user.Name:
|
|
|
|
contentType = "username"
|
2022-03-15 12:54:57 +08:00
|
|
|
respUser.Email = util.GetMaskedEmail(user.Email)
|
|
|
|
respUser.Phone = util.GetMaskedPhone(user.Phone)
|
2021-06-07 19:12:51 +08:00
|
|
|
}
|
2021-06-02 13:39:01 +08:00
|
|
|
|
2021-08-08 16:00:19 +08:00
|
|
|
c.ResponseOk(respUser, contentType)
|
2021-06-02 13:39:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// SetPassword
|
2021-05-09 19:54:20 +08:00
|
|
|
// @Title SetPassword
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
2021-05-09 19:54:20 +08:00
|
|
|
// @Description set password
|
|
|
|
// @Param userOwner formData string true "The owner of the user"
|
|
|
|
// @Param userName formData string true "The name of the user"
|
|
|
|
// @Param oldPassword formData string true "The old password of the user"
|
|
|
|
// @Param newPassword formData string true "The new password of the user"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /set-password [post]
|
|
|
|
func (c *ApiController) SetPassword() {
|
|
|
|
userOwner := c.Ctx.Request.Form.Get("userOwner")
|
|
|
|
userName := c.Ctx.Request.Form.Get("userName")
|
|
|
|
oldPassword := c.Ctx.Request.Form.Get("oldPassword")
|
|
|
|
newPassword := c.Ctx.Request.Form.Get("newPassword")
|
2023-04-06 23:06:18 +08:00
|
|
|
code := c.Ctx.Request.Form.Get("code")
|
|
|
|
|
2023-05-04 22:07:19 +08:00
|
|
|
//if userOwner == "built-in" && userName == "admin" {
|
|
|
|
// c.ResponseError(c.T("auth:Unauthorized operation"))
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
if strings.Contains(newPassword, " ") {
|
|
|
|
c.ResponseError(c.T("user:New password cannot contain blank space."))
|
|
|
|
return
|
|
|
|
}
|
2021-05-09 19:54:20 +08:00
|
|
|
|
2023-03-01 15:57:42 +08:00
|
|
|
userId := util.GetId(userOwner, userName)
|
2021-05-09 19:54:20 +08:00
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
requestUserId := c.GetSessionUsername()
|
|
|
|
if requestUserId == "" && code == "" {
|
2023-06-22 14:46:38 +08:00
|
|
|
c.ResponseError(c.T("general:Please login first"), "Please login first")
|
2021-05-09 19:54:20 +08:00
|
|
|
return
|
2023-04-06 23:06:18 +08:00
|
|
|
} else if code == "" {
|
|
|
|
hasPermission, err := object.CheckUserPermission(requestUserId, userId, true, c.GetAcceptLanguage())
|
|
|
|
if !hasPermission {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if code != c.GetSession("verifiedCode") {
|
2023-06-22 14:46:38 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
2023-04-06 23:06:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetSession("verifiedCode", "")
|
2021-05-09 19:54:20 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
targetUser, err := object.GetUser(userId)
|
2023-09-03 00:04:48 +08:00
|
|
|
if targetUser == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), userId))
|
|
|
|
return
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-04-16 15:10:03 +08:00
|
|
|
|
2023-09-15 10:21:02 +08:00
|
|
|
isAdmin := c.IsAdmin()
|
|
|
|
if isAdmin {
|
|
|
|
if oldPassword != "" {
|
|
|
|
msg := object.CheckPassword(targetUser, oldPassword, c.GetAcceptLanguage())
|
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 18:25:25 +08:00
|
|
|
} else if code == "" {
|
2022-10-23 15:16:24 +08:00
|
|
|
msg := object.CheckPassword(targetUser, oldPassword, c.GetAcceptLanguage())
|
2021-06-02 13:39:01 +08:00
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
2021-05-09 19:54:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-17 00:01:20 +08:00
|
|
|
msg := object.CheckPasswordComplexity(targetUser, newPassword)
|
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:54:20 +08:00
|
|
|
targetUser.Password = newPassword
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.SetUserField(targetUser, "password", targetUser.Password)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:57:42 +08:00
|
|
|
c.ResponseOk()
|
2021-05-09 19:54:20 +08:00
|
|
|
}
|
2021-08-15 21:57:36 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// CheckUserPassword
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Title CheckUserPassword
|
|
|
|
// @router /check-user-password [post]
|
|
|
|
// @Tag User API
|
2021-08-15 21:57:36 +08:00
|
|
|
func (c *ApiController) CheckUserPassword() {
|
|
|
|
var user object.User
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-08-15 21:57:36 +08:00
|
|
|
}
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
_, msg := object.CheckUserPassword(user.Owner, user.Name, user.Password, c.GetAcceptLanguage())
|
2021-08-15 21:57:36 +08:00
|
|
|
if msg == "" {
|
|
|
|
c.ResponseOk()
|
|
|
|
} else {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 10:43:14 +08:00
|
|
|
|
|
|
|
// GetSortedUsers
|
|
|
|
// @Title GetSortedUsers
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-11-19 10:43:14 +08:00
|
|
|
// @Description
|
|
|
|
// @Param owner query string true "The owner of users"
|
|
|
|
// @Param sorter query string true "The DB column name to sort by, e.g., created_time"
|
|
|
|
// @Param limit query string true "The count of users to return, e.g., 25"
|
|
|
|
// @Success 200 {array} object.User The Response object
|
|
|
|
// @router /get-sorted-users [get]
|
|
|
|
func (c *ApiController) GetSortedUsers() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
sorter := c.Input().Get("sorter")
|
|
|
|
limit := util.ParseInt(c.Input().Get("limit"))
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
maskedUsers, err := object.GetMaskedUsers(object.GetSortedUsers(owner, sorter, limit))
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 14:46:38 +08:00
|
|
|
c.ResponseOk(maskedUsers)
|
2021-11-19 10:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserCount
|
|
|
|
// @Title GetUserCount
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag User API
|
2021-11-19 10:43:14 +08:00
|
|
|
// @Description
|
|
|
|
// @Param owner query string true "The owner of users"
|
|
|
|
// @Param isOnline query string true "The filter for query, 1 for online, 0 for offline, empty string for all users"
|
|
|
|
// @Success 200 {int} int The count of filtered users for an organization
|
|
|
|
// @router /get-user-count [get]
|
|
|
|
func (c *ApiController) GetUserCount() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
isOnline := c.Input().Get("isOnline")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
var count int64
|
|
|
|
var err error
|
2021-11-19 10:43:14 +08:00
|
|
|
if isOnline == "" {
|
2023-06-12 09:27:16 +08:00
|
|
|
count, err = object.GetUserCount(owner, "", "", "")
|
2021-11-19 10:43:14 +08:00
|
|
|
} else {
|
2023-05-30 15:49:39 +08:00
|
|
|
count, err = object.GetOnlineUserCount(owner, util.ParseInt(isOnline))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-11-19 10:43:14 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 14:46:38 +08:00
|
|
|
c.ResponseOk(count)
|
2021-11-19 10:43:14 +08:00
|
|
|
}
|
2023-06-13 22:18:17 +08:00
|
|
|
|
2023-10-31 13:23:06 +08:00
|
|
|
// AddUserKeys
|
|
|
|
// @Title AddUserKeys
|
2023-06-13 22:18:17 +08:00
|
|
|
// @router /add-user-keys [post]
|
|
|
|
// @Tag User API
|
2023-10-31 13:23:06 +08:00
|
|
|
func (c *ApiController) AddUserKeys() {
|
2023-06-13 22:18:17 +08:00
|
|
|
var user object.User
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isAdmin := c.IsAdmin()
|
2023-10-31 13:23:06 +08:00
|
|
|
affected, err := object.AddUserKeys(&user, isAdmin)
|
2023-06-13 22:18:17 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResponseOk(affected)
|
|
|
|
}
|
2023-06-14 23:27:46 +08:00
|
|
|
|
|
|
|
func (c *ApiController) RemoveUserFromGroup() {
|
|
|
|
owner := c.Ctx.Request.Form.Get("owner")
|
|
|
|
name := c.Ctx.Request.Form.Get("name")
|
2023-06-19 19:08:45 +08:00
|
|
|
groupName := c.Ctx.Request.Form.Get("groupName")
|
2023-06-14 23:27:46 +08:00
|
|
|
|
2023-08-11 10:59:18 +08:00
|
|
|
organization, err := object.GetOrganization(util.GetId("admin", owner))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := object.GetAccountItemByName("Groups", organization)
|
|
|
|
res, msg := object.CheckAccountItemModifyRule(item, c.IsAdmin(), c.GetAcceptLanguage())
|
|
|
|
if !res {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := object.DeleteGroupForUser(util.GetId(owner, name), groupName)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResponseOk(affected)
|
2023-06-14 23:27:46 +08:00
|
|
|
}
|