2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-07-17 14:13:00 +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-12-03 20:42:36 +08:00
|
|
|
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-07-17 14:13:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type LdapResp struct {
|
2022-08-07 12:26:14 +08:00
|
|
|
// Groups []LdapRespGroup `json:"groups"`
|
2023-05-19 02:34:25 +08:00
|
|
|
Users []object.LdapUser `json:"users"`
|
|
|
|
ExistUuids []string `json:"existUuids"`
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//type LdapRespGroup struct {
|
|
|
|
// GroupId string
|
|
|
|
// GroupName string
|
|
|
|
//}
|
|
|
|
|
|
|
|
type LdapSyncResp struct {
|
2023-05-19 02:34:25 +08:00
|
|
|
Exist []object.LdapUser `json:"exist"`
|
|
|
|
Failed []object.LdapUser `json:"failed"`
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 11:12:51 +08:00
|
|
|
// GetLdapUsers
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title GetLdapser
|
2023-03-12 11:12:51 +08:00
|
|
|
// @router /get-ldap-users [get]
|
|
|
|
func (c *ApiController) GetLdapUsers() {
|
|
|
|
id := c.Input().Get("id")
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-03-12 11:12:51 +08:00
|
|
|
_, ldapId := util.GetOwnerAndNameFromId(id)
|
2023-05-30 15:49:39 +08:00
|
|
|
ldapServer, err := object.GetLdap(ldapId)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-03-15 11:12:31 +08:00
|
|
|
conn, err := ldapServer.GetLdapConn()
|
2021-07-17 14:13:00 +08:00
|
|
|
if err != nil {
|
2021-08-08 11:06:45 +08:00
|
|
|
c.ResponseError(err.Error())
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//groupsMap, err := conn.GetLdapGroups(ldapServer.BaseDn)
|
|
|
|
//if err != nil {
|
2021-08-08 11:06:45 +08:00
|
|
|
// c.ResponseError(err.Error())
|
2021-07-17 14:13:00 +08:00
|
|
|
// return
|
|
|
|
//}
|
|
|
|
|
|
|
|
//for _, group := range groupsMap {
|
|
|
|
// resp.Groups = append(resp.Groups, LdapRespGroup{
|
|
|
|
// GroupId: group.GidNumber,
|
|
|
|
// GroupName: group.Cn,
|
|
|
|
// })
|
|
|
|
//}
|
|
|
|
|
2023-04-13 14:12:31 +08:00
|
|
|
users, err := conn.GetLdapUsers(ldapServer)
|
2021-07-17 14:13:00 +08:00
|
|
|
if err != nil {
|
2021-08-08 11:06:45 +08:00
|
|
|
c.ResponseError(err.Error())
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-12 11:12:51 +08:00
|
|
|
uuids := make([]string, len(users))
|
2023-05-19 02:34:25 +08:00
|
|
|
for i, user := range users {
|
|
|
|
uuids[i] = user.GetLdapUuid()
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
existUuids, err := object.GetExistUuids(ldapServer.Owner, uuids)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-03-12 11:12:51 +08:00
|
|
|
|
2023-05-19 02:34:25 +08:00
|
|
|
resp := LdapResp{
|
|
|
|
Users: object.AutoAdjustLdapUser(users),
|
|
|
|
ExistUuids: existUuids,
|
|
|
|
}
|
|
|
|
c.ResponseOk(resp)
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// GetLdaps
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title GetLdaps
|
2023-02-18 16:27:47 +08:00
|
|
|
// @router /get-ldaps [get]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) GetLdaps() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
|
2023-07-12 23:21:47 +08:00
|
|
|
c.ResponseOk(object.GetMaskedLdaps(object.GetLdaps(owner)))
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// GetLdap
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title GetLdap
|
2023-02-18 16:27:47 +08:00
|
|
|
// @router /get-ldap [get]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) GetLdap() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
2023-02-18 09:31:58 +08:00
|
|
|
if util.IsStringsEmpty(id) {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:31:55 +08:00
|
|
|
_, name := util.GetOwnerAndNameFromId(id)
|
2023-07-12 23:21:47 +08:00
|
|
|
c.ResponseOk(object.GetMaskedLdap(object.GetLdap(name)))
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// AddLdap
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title AddLdap
|
|
|
|
// @router /add-ldap [post]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) AddLdap() {
|
|
|
|
var ldap object.Ldap
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldap)
|
|
|
|
if err != nil {
|
2023-03-12 11:12:51 +08:00
|
|
|
c.ResponseError(err.Error())
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:12:31 +08:00
|
|
|
if util.IsStringsEmpty(ldap.Owner, ldap.ServerName, ldap.Host, ldap.Username, ldap.Password, ldap.BaseDn) {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
if ok, err := object.CheckLdapExist(&ldap); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
} else if ok {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("ldap:Ldap server exist"))
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
resp := wrapActionResponse(object.AddLdap(&ldap))
|
2023-03-12 11:12:51 +08:00
|
|
|
resp.Data2 = ldap
|
|
|
|
|
2021-12-15 17:45:11 +08:00
|
|
|
if ldap.AutoSync != 0 {
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.GetLdapAutoSynchronizer().StartAutoSync(ldap.Id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-12-15 17:45:11 +08:00
|
|
|
}
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-03-12 11:12:51 +08:00
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// UpdateLdap
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title UpdateLdap
|
|
|
|
// @router /update-ldap [post]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) UpdateLdap() {
|
|
|
|
var ldap object.Ldap
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldap)
|
2023-04-13 14:12:31 +08:00
|
|
|
if err != nil || util.IsStringsEmpty(ldap.Owner, ldap.ServerName, ldap.Host, ldap.Username, ldap.Password, ldap.BaseDn) {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
2021-07-17 14:13:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
prevLdap, err := object.GetLdap(ldap.Id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := object.UpdateLdap(&ldap)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-03-12 11:12:51 +08:00
|
|
|
|
2021-12-15 17:45:11 +08:00
|
|
|
if ldap.AutoSync != 0 {
|
2023-05-30 15:49:39 +08:00
|
|
|
err := object.GetLdapAutoSynchronizer().StartAutoSync(ldap.Id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-03-20 23:21:09 +08:00
|
|
|
} else if ldap.AutoSync == 0 && prevLdap.AutoSync != 0 {
|
2022-02-15 23:03:53 +08:00
|
|
|
object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id)
|
2021-12-15 17:45:11 +08:00
|
|
|
}
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-03-12 11:12:51 +08:00
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
|
|
|
c.ServeJSON()
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// DeleteLdap
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title DeleteLdap
|
|
|
|
// @router /delete-ldap [post]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) DeleteLdap() {
|
|
|
|
var ldap object.Ldap
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ldap)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
affected, err := object.DeleteLdap(&ldap)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-03-12 11:12:51 +08:00
|
|
|
|
2021-12-15 17:45:11 +08:00
|
|
|
object.GetLdapAutoSynchronizer().StopAutoSync(ldap.Id)
|
2023-03-12 11:12:51 +08:00
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
|
|
|
c.ServeJSON()
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// SyncLdapUsers
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title SyncLdapUsers
|
|
|
|
// @router /sync-ldap-users [post]
|
2021-07-17 14:13:00 +08:00
|
|
|
func (c *ApiController) SyncLdapUsers() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
ldapId := c.Input().Get("ldapId")
|
2023-05-19 02:34:25 +08:00
|
|
|
var users []object.LdapUser
|
2021-07-17 14:13:00 +08:00
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &users)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.UpdateLdapSyncTime(ldapId)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-05-18 22:03:53 +08:00
|
|
|
exist, failed, _ := object.SyncLdapUsers(owner, users, ldapId)
|
2023-03-08 21:31:55 +08:00
|
|
|
|
|
|
|
c.ResponseOk(&LdapSyncResp{
|
2023-05-18 22:03:53 +08:00
|
|
|
Exist: exist,
|
|
|
|
Failed: failed,
|
2023-03-08 21:31:55 +08:00
|
|
|
})
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|