http_syscfg_account.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "redisdog/model"
  12. )
  13. type SyscfgAccountListRet struct {
  14. Errno int `json:"errno"`
  15. Error string `json:"error"`
  16. Data []*model.AccountsRow `json:"data"`
  17. }
  18. type SyscfgAccountGetRet struct {
  19. Errno int `json:"errno"`
  20. Error string `json:"error"`
  21. Data *model.AccountsRow `json:"data"`
  22. }
  23. func syscfg_account(resp http.ResponseWriter, req *http.Request) {
  24. sess, ok := checkLogin(resp, req)
  25. if !ok {
  26. return
  27. }
  28. if !sess.Account.IsSuper {
  29. resp.Header().Set("Location", "/index/forbidden")
  30. resp.WriteHeader(302)
  31. return
  32. }
  33. //视图输出
  34. files := []string{
  35. filepath.Join(Cfg.TmplDir, "syscfg", "account.tmpl"),
  36. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  37. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  38. }
  39. tmpl, err := template.New("account.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  40. if err != nil {
  41. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  42. } else {
  43. tmpl.Execute(resp, struct {
  44. Sess *Session
  45. Req *http.Request
  46. Title string
  47. }{
  48. sess,
  49. req,
  50. "账号管理",
  51. })
  52. }
  53. }
  54. func syscfg_account_list(resp http.ResponseWriter, req *http.Request) {
  55. sess, ok := checkLogin(resp, req)
  56. if !ok {
  57. return
  58. }
  59. if !sess.Account.IsSuper {
  60. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  61. resp.Write(ret)
  62. return
  63. }
  64. req.ParseForm()
  65. mdlAccounts := model.NewAccounts(Db)
  66. list, err := mdlAccounts.GetAll()
  67. if err != nil {
  68. resp.Write(ERROR_RET(1, err.Error()))
  69. } else {
  70. ret, _ := json.Marshal(SyscfgAccountListRet{Errno: 0, Error: "", Data: list})
  71. resp.Write(ret)
  72. }
  73. }
  74. func syscfg_account_get(resp http.ResponseWriter, req *http.Request) {
  75. sess, ok := checkLogin(resp, req)
  76. if !ok {
  77. return
  78. }
  79. if !sess.Account.IsSuper {
  80. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  81. resp.Write(ret)
  82. return
  83. }
  84. req.ParseForm()
  85. idStr := req.Form.Get("id")
  86. if idStr == "" {
  87. resp.Write(ERROR_RET(1, "ID不能为空!"))
  88. return
  89. }
  90. id, err := strconv.Atoi(idStr)
  91. if err != nil {
  92. resp.Write(ERROR_RET(2, err.Error()))
  93. return
  94. }
  95. mdlAccounts := model.NewAccounts(Db)
  96. item, err := mdlAccounts.Get(int64(id))
  97. if err != nil {
  98. resp.Write(ERROR_RET(3, err.Error()))
  99. } else {
  100. ret, _ := json.Marshal(SyscfgAccountGetRet{Errno: 0, Error: "", Data: item})
  101. resp.Write(ret)
  102. }
  103. }
  104. func syscfg_account_set(resp http.ResponseWriter, req *http.Request) {
  105. sess, ok := checkLogin(resp, req)
  106. if !ok {
  107. return
  108. }
  109. if !sess.Account.IsSuper {
  110. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  111. resp.Write(ret)
  112. return
  113. }
  114. req.ParseForm()
  115. Id := req.PostForm.Get("Id")
  116. Account := req.PostForm.Get("Account")
  117. Name := req.PostForm.Get("Name")
  118. Password := req.PostForm.Get("Password")
  119. IsSuper := req.PostForm.Get("IsSuper")
  120. Disabled := req.PostForm.Get("Disabled")
  121. IdInt, err := strconv.Atoi(Id)
  122. if err != nil {
  123. IdInt = 0
  124. }
  125. if Account == "" {
  126. resp.Write(ERROR_RET(1, "账号不能为空!"))
  127. return
  128. }
  129. reg := regexp.MustCompile(`^[A-Za-z]\w{1,19}$`)
  130. if !reg.MatchString(Account) {
  131. resp.Write(ERROR_RET(2, "账号格式不正确!"))
  132. return
  133. }
  134. if Name == "" {
  135. resp.Write(ERROR_RET(3, "姓名不能为空!"))
  136. return
  137. }
  138. if IdInt == 0 {
  139. if Password == "" {
  140. resp.Write(ERROR_RET(4, "密码不能为空!"))
  141. return
  142. }
  143. if len(Password) < 6 {
  144. resp.Write(ERROR_RET(5, "密码长度不能小于6!"))
  145. return
  146. }
  147. }
  148. if IsSuper == "" {
  149. resp.Write(ERROR_RET(6, "请选择是否管理员!"))
  150. return
  151. }
  152. if Disabled == "" {
  153. resp.Write(ERROR_RET(7, "状态不能为空!"))
  154. return
  155. }
  156. IsSuperInt, err := strconv.Atoi(IsSuper)
  157. if err != nil {
  158. resp.Write(ERROR_RET(8, err.Error()))
  159. return
  160. }
  161. DisabledInt, err := strconv.Atoi(Disabled)
  162. if err != nil {
  163. resp.Write(ERROR_RET(9, err.Error()))
  164. return
  165. }
  166. newRow := model.AccountsRow{
  167. Id: int64(IdInt),
  168. Account: Account,
  169. Name: Name,
  170. Password: Password,
  171. IsSuper: IsSuperInt != 0,
  172. Disabled: DisabledInt != 0,
  173. }
  174. mdlAccounts := model.NewAccounts(Db)
  175. if IdInt > 0 {
  176. affected, err := mdlAccounts.Update(&newRow)
  177. if err != nil {
  178. resp.Write(ERROR_RET(10, err.Error()))
  179. } else if affected > 0 {
  180. resp.Write(ERROR_RET(0, ""))
  181. } else {
  182. resp.Write(ERROR_RET(11, "更新失败!"))
  183. }
  184. } else {
  185. newRowId, err := mdlAccounts.Insert(&newRow)
  186. if err != nil {
  187. resp.Write(ERROR_RET(22, err.Error()))
  188. } else if newRowId > 0 {
  189. ret, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId})
  190. resp.Write(ret)
  191. } else {
  192. resp.Write(ERROR_RET(23, "新增失败!"))
  193. }
  194. }
  195. }
  196. func syscfg_account_del(resp http.ResponseWriter, req *http.Request) {
  197. sess, ok := checkLogin(resp, req)
  198. if !ok {
  199. return
  200. }
  201. if !sess.Account.IsSuper {
  202. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  203. resp.Write(ret)
  204. return
  205. }
  206. req.ParseForm()
  207. idStr := req.Form.Get("id")
  208. if idStr == "" {
  209. resp.Write(ERROR_RET(1, "ID不能为空!"))
  210. return
  211. }
  212. id, err := strconv.Atoi(idStr)
  213. if err != nil {
  214. resp.Write(ERROR_RET(2, err.Error()))
  215. return
  216. }
  217. mdlAccounts := model.NewAccounts(Db)
  218. affected, err := mdlAccounts.Delete(int64(id))
  219. if err != nil {
  220. resp.Write(ERROR_RET(3, err.Error()))
  221. } else if affected > 0 {
  222. resp.Write(ERROR_RET(0, ""))
  223. SYSLOG("WARN", fmt.Sprintf("管理员#%d %s %s删除了账号 #%d", sess.Account.Id, sess.Account.Name, sess.Account.Account, id))
  224. } else {
  225. resp.Write(ERROR_RET(4, "操作失败!"))
  226. }
  227. }
  228. func syscfg_account_reset_pwd(resp http.ResponseWriter, req *http.Request) {
  229. sess, ok := checkLogin(resp, req)
  230. if !ok {
  231. return
  232. }
  233. if !sess.Account.IsSuper {
  234. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  235. resp.Write(ret)
  236. return
  237. }
  238. req.ParseForm()
  239. idStr := req.PostForm.Get("id")
  240. if idStr == "" {
  241. resp.Write(ERROR_RET(1, "ID不能为空!"))
  242. return
  243. }
  244. id, err := strconv.Atoi(idStr)
  245. if err != nil {
  246. resp.Write(ERROR_RET(2, err.Error()))
  247. return
  248. }
  249. password := req.PostForm.Get("password")
  250. if len(password) < 6 {
  251. resp.Write(ERROR_RET(3, "密码长度不能小于6位!"))
  252. return
  253. }
  254. mdlAccounts := model.NewAccounts(Db)
  255. affected, err := mdlAccounts.UpdatePassword(int64(id), password)
  256. if err != nil {
  257. resp.Write(ERROR_RET(4, err.Error()))
  258. } else if affected > 0 {
  259. resp.Write(ERROR_RET(0, ""))
  260. SYSLOG("WARN", fmt.Sprintf("管理员#%d %s %s修改了账号 #%d的密码", sess.Account.Id, sess.Account.Name, sess.Account.Account, id))
  261. } else {
  262. resp.Write(ERROR_RET(5, "操作失败!"))
  263. }
  264. }