account.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package auth
  2. // var AuthAccounts = make(map[string]string)
  3. /* 20230419 pms 删除 =======================================================================================================
  4. // Login 登录认证
  5. // @tags PBX-Auth
  6. // @Summary 登录认证
  7. // @Description 获取登录参数(账号,密码,角色) 与数据库数据对比,认证通过返回 jwt token
  8. // @Accept json
  9. // @Produce json
  10. // @Param data body commonModel.UserInfoReqVO true "登录信息"
  11. // @Router /pbx/auth/login [post]
  12. func Login(ctx *gin.Context) {
  13. var userReqInfo commonModel.UserInfoReqVO
  14. if err := ctx.ShouldBindJSON(&userReqInfo); err != nil {
  15. api.Error(ctx, http.StatusBadRequest, err.Error())
  16. return
  17. }
  18. lfshook.NewLogger().Infof("user input info: %+v", userReqInfo)
  19. var info *commonModel.UserInfoVO
  20. // 用户名转换成分机号(数字)
  21. var err error
  22. result := utils.IsDigit(userReqInfo.UserName)
  23. if !result {
  24. // 用户登录
  25. lfshook.NewLogger().Info("login by user")
  26. info, err = services.AuthUser(userReqInfo.UserName, userReqInfo.Password)
  27. } else {
  28. // 分机登录
  29. lfshook.NewLogger().Info("login by digital")
  30. info, err = services.AuthDigital(userReqInfo.UserName, userReqInfo.Password)
  31. }
  32. if err != nil {
  33. weblog.AuthError(ctx.ClientIP(), err.Error())
  34. api.Error(ctx, http.StatusBadRequest, err.Error())
  35. return
  36. }
  37. exp := time.Now().Add(time.Duration(time.Hour * 24 * 31)).Unix()
  38. claim := commonModel.JWTCustomClaims{
  39. ID: info.ID,
  40. UserName: info.UserName,
  41. Role: info.Role,
  42. Extension: info.Exten,
  43. StandardClaims: jwt.StandardClaims{
  44. ExpiresAt: exp,
  45. },
  46. }
  47. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)
  48. tokenString, err := token.SignedString([]byte(configs.ConfigGlobal.IdentityKey))
  49. if err != nil {
  50. lfshook.NewLogger().Error(err)
  51. api.Error(ctx, http.StatusBadRequest, "signed failure")
  52. return
  53. }
  54. api.Success(ctx, fmt.Sprintf("Bearer %s", tokenString))
  55. }
  56. * ========================================================================================================================= */
  57. /* 20250102 删除 =======================================================================================================
  58. // // AddAuth pms用户认证
  59. // // @tags PBX-Auth
  60. // // @Summary pms用户认证
  61. // // @Description 从t_user表中抽出数据,添加到用户认证
  62. // // @Security ApiKeyAuth
  63. // func AddAuth() {
  64. // // 取表 t_user 中用户
  65. // /*var dbUser []commonModel.User
  66. // if err := mysql.DBOrmInstance.Find(&dbUser); err != nil {
  67. // lfshook.NewLogger().Error(err)
  68. // }
  69. // for _, item := range dbUser {
  70. // AuthAccounts[item.UserName] = item.PassWord
  71. // }
  72. // fmt.Printf("extenList=%s\n", AuthAccounts)*/
  73. // // 设计变更
  74. // // 读取pms配置文件中的用户密码
  75. // confPath := "/etc/asterisk/pms_api.conf"
  76. // cfg, err := ini.Load(confPath)
  77. // if err != nil {
  78. // lfshook.NewLogger().Error(err)
  79. // return
  80. // }
  81. // UserName := cfg.Section("general").Key("username").String()
  82. // PassWord := cfg.Section("general").Key("password").String()
  83. // if UserName == "" || PassWord == "" {
  84. // lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set username or password")
  85. // return
  86. // }
  87. // AuthAccounts[UserName] = PassWord
  88. // // fmt.Printf("extenList=%s\n", AuthAccounts)
  89. // }
  90. // * ========================================================================================================================= */
  91. /* 20250102 删除 =======================================================================================================
  92. // VtigerAddAuth vtiger用户认证
  93. // @tags PBX-Auth
  94. // @Summary vtiger用户认证
  95. // @Description 从配置文件中取出数据,添加到用户认证
  96. // @Security ApiKeyAuth
  97. func VtigerAddAuth() {
  98. // 读取vtiger配置文件中的用户密码
  99. // confPath := "/etc/asterisk/vtiger_api.conf"
  100. confPath := "/etc/asterisk/crm_api.conf"
  101. cfg, err := ini.Load(confPath)
  102. if err != nil {
  103. lfshook.NewLogger().Error(err)
  104. return
  105. }
  106. BasicAuthUser := cfg.Section("general").Key("vtigerBasicAuthUser").String()
  107. BasicAuthPWD := cfg.Section("general").Key("vtigerBasicAuthPWD").String()
  108. ApiKey := cfg.Section("general").Key("vtigerApiKey").String()
  109. ApiKeyValue := cfg.Section("general").Key("vtigerApiKeyValue").String()
  110. if (BasicAuthUser == "" || BasicAuthPWD == "") && (ApiKey == "" || ApiKeyValue == "") {
  111. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set vtigerBasicAuth or vtigerApiKey")
  112. return
  113. }
  114. AuthAccounts[BasicAuthUser] = BasicAuthPWD
  115. AuthAccounts[ApiKey] = ApiKeyValue
  116. // fmt.Printf("AuthAccounts=%s\n", AuthAccounts)
  117. }
  118. // ZohoAddAuth zoho用户认证
  119. // @tags PBX-Auth
  120. // @Summary zoho用户认证
  121. // @Description 从配置文件中取出数据,添加到用户认证
  122. // @Security ApiKeyAuth
  123. func ZohoAddAuth() {
  124. // 读取vtiger配置文件中的用户密码
  125. // confPath := "/etc/asterisk/vtiger_api.conf"
  126. confPath := "/etc/asterisk/crm_api.conf"
  127. cfg, err := ini.Load(confPath)
  128. if err != nil {
  129. lfshook.NewLogger().Error(err)
  130. return
  131. }
  132. BasicAuthUser := cfg.Section("general").Key("zohoBasicAuthUser").String()
  133. BasicAuthPWD := cfg.Section("general").Key("zohoBasicAuthPWD").String()
  134. if BasicAuthUser == "" || BasicAuthPWD == "" {
  135. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoBasicAuthUser or zohoBasicAuthPWD")
  136. return
  137. }
  138. AuthAccounts[BasicAuthUser] = BasicAuthPWD
  139. // fmt.Printf("AuthAccounts=%s\n", AuthAccounts)
  140. }
  141. // * ========================================================================================================================= */
  142. /* 20230419 pms 删除 =======================================================================================================
  143. // Logout 注销登录
  144. // @tags PBX-Auth
  145. // @Summary 注销登录
  146. // @Description 将 token 写入 redis, 标记注销。此 token 认证失效。
  147. // @Security ApiKeyAuth
  148. // @Accept json
  149. // @Produce json
  150. // @Router /pbx/auth/logout [get]
  151. func Logout(ctx *gin.Context) {
  152. ID, _ := ctx.Get("ID")
  153. UserName, _ := ctx.Get("UserName")
  154. authorization := ctx.GetHeader("Authorization")
  155. auth.Logout(authorization)
  156. api.Success(ctx, map[string]string{"ID": strconv.FormatInt(ID.(int64), 10), "UserName": UserName.(string)})
  157. }
  158. // CurrentUser 当前用户
  159. // @tags PBX-Auth
  160. // @Summary 当前用户
  161. // @Description 获取当前用户信息ID UserName Password Role UserExtension(关联的分机号) PanelQueueuNumber(关联的随机一个队列号)
  162. // @Security ApiKeyAuth
  163. // @Accept json
  164. // @Produce json
  165. // @Router /pbx/auth/currentuser [get]
  166. func CurrentUser(ctx *gin.Context) {
  167. ID, _ := ctx.Get("ID")
  168. if ID == nil {
  169. lfshook.NewLogger().Error("currentUser get no not found")
  170. api.Error(ctx, http.StatusInternalServerError, "not found id")
  171. return
  172. }
  173. idInt64 := ID.(int64)
  174. lfshook.NewLogger().Infof("currentUser id: %d", idInt64)
  175. dbUser := mysql.GetUserInfoByID(idInt64)
  176. // 根据分机号查询关联队列
  177. data, err := mysql.DBOrmInstance.SQL("select queue_name from t_queue_agent where exten = ? and agent_type='static'", dbUser.UserExtension).QueryString()
  178. if err != nil {
  179. lfshook.NewLogger().Errorf("currentUser get queue %+v", err)
  180. }
  181. if len(data) > 0 {
  182. dbUser.PanelQueueNumber = data[0]["queue_name"]
  183. }
  184. api.Success(ctx, dbUser)
  185. }
  186. // @tags PBX-Auth
  187. // @Summary 更新密码
  188. // @Description 更新当前用户密码
  189. // @Security ApiKeyAuth
  190. // @Accept json
  191. // @Produce json
  192. // @Router /pbx/auth/update-password [post]
  193. func UpdatePassword(ctx *gin.Context) {
  194. var info commonModel.UserPasswordVO
  195. if err := ctx.ShouldBind(&info); err != nil {
  196. api.Error(ctx, http.StatusBadRequest, err.Error())
  197. return
  198. }
  199. ID, _ := ctx.Get("ID")
  200. if ID == nil {
  201. lfshook.NewLogger().Error("currentUser get no not found")
  202. api.Error(ctx, http.StatusInternalServerError, "not found id")
  203. return
  204. }
  205. idInt64 := ID.(int64)
  206. user := commonModel.User{ID: idInt64}
  207. exist, err := mysql.DBOrmInstance.Get(&user)
  208. if err != nil {
  209. api.Error(ctx, http.StatusInternalServerError, err.Error())
  210. return
  211. }
  212. if !exist {
  213. api.Error(ctx, http.StatusInternalServerError, "not found user by id "+strconv.FormatInt(idInt64, 10))
  214. return
  215. }
  216. if user.PassWord != info.OldPassWord {
  217. api.Error(ctx, http.StatusInternalServerError, "old password error")
  218. return
  219. }
  220. user.PassWord = info.NewPassWord
  221. _, err = mysql.DBOrmInstance.ID(idInt64).Cols("password").Update(&user)
  222. if err != nil {
  223. api.Error(ctx, http.StatusInternalServerError, err.Error())
  224. return
  225. }
  226. api.Success(ctx, "success")
  227. }
  228. * ========================================================================================================================= */