service.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package ldap
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "github.com/astaxie/beego/orm"
  8. "nginx-ui/server/config"
  9. "nginx-ui/server/models"
  10. "nginx-ui/server/vo"
  11. )
  12. type Service struct {
  13. }
  14. var ServiceInstance = new(Service)
  15. var UserIns = new(UserService)
  16. // GetServer 获取一个可用的LDAP 连接, 用于登录时获取服务信息
  17. func (c *Service) GetServer() (*models.LdapServer, error) {
  18. o := orm.NewOrm()
  19. server := models.LdapServer{
  20. Active: true,
  21. }
  22. err := o.Read(&server, "Active")
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &server, nil
  27. }
  28. func (c *Service) Login(req *LDAPLoginReq) (*models.User, error) {
  29. server := models.LdapServer{Key: req.ServerKey}
  30. o := orm.NewOrm()
  31. err := o.Read(&server, "Key")
  32. if err != nil {
  33. return nil, errors.New("未找到对应的LDAP服务!")
  34. }
  35. user, err := UserIns.Authentication(&server, req.Account, req.Password)
  36. if err != nil {
  37. return nil, err
  38. }
  39. user.Password = ""
  40. return user, nil
  41. }
  42. // GetServers 获取用户所有的LDAP连接
  43. // get /ldap/server
  44. func (c *Service) GetServers(current *models.User, req *vo.PageReq) (*vo.PageResp, error) {
  45. o := orm.NewOrm()
  46. req.Ensure()
  47. qs := o.QueryTable(&models.LdapServer{})
  48. if !current.IsAdmin() {
  49. qs = qs.Filter("Uid", current.Account)
  50. }
  51. total, err := qs.Count()
  52. if err != nil {
  53. return nil, err
  54. }
  55. qs.OrderBy("Id")
  56. qs.Offset(req.Offset)
  57. qs.Limit(req.PageSize)
  58. var list []*models.LdapServer
  59. _, err = qs.All(&list)
  60. for _, v := range list {
  61. v.Password = config.ReplacePassword
  62. }
  63. if err != nil {
  64. return nil, err
  65. }
  66. resp := vo.PageResp{
  67. Current: req.Current,
  68. PageSize: req.PageSize,
  69. Total: total,
  70. List: list,
  71. }
  72. return &resp, err
  73. }
  74. // Update 保存或者修改
  75. // post /ldap/server
  76. func (c *Service) Update(current *models.User, body *models.LdapServer) (*models.LdapServer, error) {
  77. body.Uid = string(rune(current.Id))
  78. if body.Url == "" {
  79. return nil, errors.New("请完成服务配置,缺少Url!")
  80. }
  81. if body.Key == "" {
  82. key := md5.Sum([]byte(body.Url))
  83. body.Key = hex.EncodeToString(key[:])
  84. }
  85. o := orm.NewOrm()
  86. if body.Id == 0 {
  87. exist := models.LdapServer{Key: body.Key}
  88. err := o.Read(&exist, "Key")
  89. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  90. return nil, err
  91. }
  92. if exist.Id > 0 {
  93. return nil, errors.New("该服务Url已存在!")
  94. }
  95. }
  96. if body.Id > 0 {
  97. exist := models.LdapServer{Id: body.Id}
  98. err := o.Read(&exist, "Id")
  99. if err != nil {
  100. return nil, err
  101. }
  102. if config.ReplacePassword == body.Password {
  103. body.Password = exist.Password
  104. }
  105. _, err = o.Update(body)
  106. if err != nil {
  107. return nil, err
  108. }
  109. } else {
  110. id, err := o.Insert(body)
  111. if err != nil {
  112. return nil, err
  113. }
  114. body.Id = int(id)
  115. }
  116. CloseActiveClient(body)
  117. return body, nil
  118. }
  119. // Add Update 保存或者修改
  120. func (c *Service) Add(current *models.User, body *models.LdapServer) (*models.LdapServer, error) {
  121. if body.Url == "" {
  122. return nil, errors.New("请完成服务配置,缺少Url!")
  123. }
  124. if body.Key == "" {
  125. key := md5.Sum([]byte(body.Url))
  126. body.Key = hex.EncodeToString(key[:])
  127. }
  128. o := orm.NewOrm()
  129. if body.Id == 0 {
  130. exist := models.LdapServer{Key: body.Key}
  131. err := o.Read(&exist, "Key")
  132. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  133. return nil, err
  134. }
  135. if exist.Id > 0 {
  136. return nil, errors.New("该服务Url已存在!")
  137. }
  138. }
  139. if body.Id > 0 {
  140. exist := models.LdapServer{Id: body.Id}
  141. err := o.Read(&exist, "Id")
  142. if err != nil {
  143. return nil, err
  144. }
  145. if config.ReplacePassword == body.Password {
  146. body.Password = exist.Password
  147. }
  148. _, err = o.Update(body)
  149. if err != nil {
  150. return nil, err
  151. }
  152. } else {
  153. id, err := o.Insert(body)
  154. if err != nil {
  155. return nil, err
  156. }
  157. body.Id = int(id)
  158. }
  159. return body, nil
  160. }
  161. // VerifyServer 验证服务
  162. func (c *Service) VerifyServer(req *VerifyReq) ([]*models.LdapUser, error) {
  163. var server = &models.LdapServer{
  164. Id: req.Id,
  165. }
  166. o := orm.NewOrm()
  167. err := o.Read(server, "Id")
  168. if err != nil {
  169. return nil, err
  170. }
  171. if req.Filter == "" && req.Username != "" {
  172. req.Filter = fmt.Sprintf("(&(objectClass=*)(uid=%s))", req.Username)
  173. }
  174. users, _, err := UserIns.Search(server, req.Filter)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return users, nil
  179. }
  180. // GetUsers 获取全部用户
  181. // get /ldap/users
  182. func (c *Service) GetUsers(current *models.User, req *UserListReq) (*vo.PageResp, error) {
  183. req.Ensure()
  184. o := orm.NewOrm()
  185. qs := o.QueryTable(&models.LdapUser{})
  186. if !current.IsAdmin() {
  187. qs = qs.Filter("Uid", current.Account)
  188. }
  189. qs.Filter("ServerKey", req.ServerKey)
  190. total, err := qs.Count()
  191. if err != nil {
  192. return nil, err
  193. }
  194. qs.OrderBy("Id")
  195. qs.Offset(req.Offset)
  196. qs.Limit(req.PageSize)
  197. var list []*models.LdapUser
  198. _, err = qs.All(&list)
  199. if err != nil {
  200. return nil, err
  201. }
  202. resp := vo.PageResp{
  203. Current: req.Current,
  204. PageSize: req.PageSize,
  205. Total: total,
  206. List: list,
  207. }
  208. return &resp, nil
  209. }