administrator.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/metarare/metarare_api/common"
  6. "github.com/metarare/metarare_api/helpers/gauth"
  7. "github.com/metarare/metarare_api/helpers/gerror"
  8. "github.com/metarare/metarare_api/models"
  9. "github.com/metarare/metarare_api/view"
  10. "gorm.io/gorm"
  11. )
  12. type AdminAdministratorV1Router struct {
  13. group *gin.RouterGroup
  14. mDB *gorm.DB
  15. rDB *gorm.DB
  16. }
  17. func NewAdminAdministratorV1Router(r common.Router, basePath string) AdminAdministratorV1Router {
  18. a := AdminAdministratorV1Router{
  19. group: r.Version.Group(basePath),
  20. mDB: r.Db.MasterDB,
  21. rDB: r.Db.ReadDB,
  22. }
  23. a.group.GET("", a.getAdministorList)
  24. a.group.PATCH("status", a.updateAdministratorStatus)
  25. a.group.POST("", a.registerAdministor)
  26. a.group.PATCH("permission", a.updatePermission)
  27. return a
  28. }
  29. // getAdministorList godoc
  30. // @Summary get administrator list
  31. // @Description 관리자 리스트 가져오기
  32. // @Schemes
  33. // @security ApiKeyAuth
  34. // @Tags admin
  35. // @Accept json
  36. // @Produce json
  37. // @Success 200 {object} view.AdministratorList
  38. // @Router /admin/administrator [get]
  39. func (a AdminAdministratorV1Router) getAdministorList(c *gin.Context) {
  40. admin, err := gauth.ConfirmAdminInfo(c, a.rDB)
  41. if err != nil || admin.ID == 0 {
  42. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  43. return
  44. }
  45. if admin.AdminPermission.Admin == 0 {
  46. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  47. return
  48. }
  49. response := []view.AdministratorList{}
  50. if err := view.GetAdministorList(a.rDB).Find(&response).Error; err != nil {
  51. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  52. return
  53. }
  54. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, response, nil)
  55. return
  56. }
  57. // updateAdministratorStatus godoc
  58. // @Summary update administrator status
  59. // @Description 관리자 상태값 변경
  60. // @Schemes
  61. // @security ApiKeyAuth
  62. // @Tags admin
  63. // @Accept json
  64. // @Produce json
  65. // @Param UpdateStatus body UpdateStatus true "selected target data"
  66. // @Success 200 {string} OK
  67. // @Router /admin/administrator/status [patch]
  68. func (a AdminAdministratorV1Router) updateAdministratorStatus(c *gin.Context) {
  69. admin, err := gauth.ConfirmAdminInfo(c, a.rDB)
  70. if err != nil || admin.ID == 0 {
  71. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  72. return
  73. }
  74. if admin.AdminPermission.Admin == 0 {
  75. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  76. return
  77. }
  78. request := UpdateStatus{}
  79. if err := c.ShouldBindJSON(&request); err != nil {
  80. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  81. return
  82. }
  83. if request.Status != "stable" && request.Status != "blocked" {
  84. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  85. return
  86. }
  87. _admin := models.Admin{}
  88. if err := a.rDB.Where("id = ?", request.ID).Find(&_admin).Error; err != nil {
  89. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlContextError, nil, err)
  90. return
  91. } else if _admin.ID == 0 {
  92. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err)
  93. return
  94. }
  95. tx := a.mDB.Begin()
  96. defer common.DBTransaction(tx)
  97. //SECTION admin log
  98. log := Log{
  99. DB: tx,
  100. ActionType: "admin",
  101. SubActionType: "modified",
  102. Admin: admin,
  103. TargetID: request.ID,
  104. To: request.Status,
  105. From: _admin.Status,
  106. Target: "status",
  107. }
  108. _admin.Status = request.Status
  109. if err := tx.Save(&_admin).Error; err != nil {
  110. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  111. return
  112. }
  113. if err = StackLog(log); err != nil {
  114. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  115. tx.Rollback()
  116. return
  117. }
  118. if err := tx.Commit().Error; err != nil {
  119. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  120. tx.Rollback()
  121. return
  122. }
  123. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, nil)
  124. return
  125. }
  126. // registerAdministor godoc
  127. // @Summary create administrator
  128. // @Description 관리자 등록
  129. // @Schemes
  130. // @security ApiKeyAuth
  131. // @Tags admin
  132. // @Accept json
  133. // @Produce json
  134. // @Param RegisterAdministratorData body RegisterAdministratorData true "body struct"
  135. // @Success 200 {number} _admin.ID
  136. // @Router /admin/administrator [post]
  137. func (a AdminAdministratorV1Router) registerAdministor(c *gin.Context) {
  138. admin, err := gauth.ConfirmAdminInfo(c, a.rDB)
  139. if err != nil || admin.ID == 0 {
  140. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  141. return
  142. }
  143. if admin.AdminPermission.Admin == 0 {
  144. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  145. return
  146. }
  147. request := RegisterAdministratorData{}
  148. if err := c.ShouldBindJSON(&request); err != nil {
  149. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  150. return
  151. }
  152. tx := a.mDB.Begin()
  153. defer common.DBTransaction(tx)
  154. _admin := models.Admin{
  155. Name: request.Name,
  156. Status: "stable",
  157. Email: request.Email,
  158. Password: request.Password,
  159. Phone: request.Phone,
  160. Position: request.Position,
  161. Chargeof: request.Chargeof,
  162. Team: request.Team,
  163. }
  164. if err := a.mDB.Save(&_admin).Error; err != nil {
  165. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.MysqlSaveError, nil, err)
  166. tx.Rollback()
  167. return
  168. }
  169. permission := models.AdminPermission{
  170. AdminID: _admin.ID,
  171. }
  172. if err := a.mDB.Save(&permission).Error; err != nil {
  173. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.MysqlSaveError, nil, err)
  174. tx.Rollback()
  175. return
  176. }
  177. //SECTION admin log
  178. log := Log{
  179. DB: tx,
  180. ActionType: "admin",
  181. SubActionType: "created",
  182. Admin: admin,
  183. TargetID: _admin.ID,
  184. }
  185. if err = StackLog(log); err != nil {
  186. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  187. tx.Rollback()
  188. return
  189. }
  190. if err := tx.Commit().Error; err != nil {
  191. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  192. tx.Rollback()
  193. return
  194. }
  195. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, _admin.ID, nil)
  196. return
  197. }
  198. // updatePermission godoc
  199. // @Summary update administrator permmsion
  200. // @Description 관리자 권한 변경
  201. // @Schemes
  202. // @security ApiKeyAuth
  203. // @Tags admin
  204. // @Accept json
  205. // @Produce json
  206. // @Param UpdatePermission body UpdatePermission true "selected target data"
  207. // @Success 200 {string} OK
  208. // @Router /admin/administrator/permission [patch]
  209. func (a AdminAdministratorV1Router) updatePermission(c *gin.Context) {
  210. admin, err := gauth.ConfirmAdminInfo(c, a.rDB)
  211. if err != nil || admin.ID == 0 {
  212. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.Unauthorized, nil, err)
  213. return
  214. }
  215. if admin.AdminPermission.Admin == 0 {
  216. gerror.IntegratedResponseToRequest(c, http.StatusUnauthorized, gerror.PermissionNotFound, nil, err)
  217. return
  218. }
  219. request := UpdatePermission{}
  220. if err := c.ShouldBindJSON(&request); err != nil {
  221. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  222. return
  223. }
  224. permission := models.AdminPermission{}
  225. if err := a.rDB.Where("admin_id = ?", request.AdminID).Find(&permission).Error; err != nil {
  226. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  227. return
  228. } else if permission.ID == 0 {
  229. gerror.IntegratedResponseToRequest(c, http.StatusNotFound, gerror.NotFoundRecord, nil, err)
  230. return
  231. }
  232. if request.UserPermission {
  233. permission.User = 1
  234. } else {
  235. permission.User = 0
  236. }
  237. if request.CollectionPermission {
  238. permission.Collection = 1
  239. } else {
  240. permission.Collection = 0
  241. }
  242. if request.SystemPermission {
  243. permission.System = 1
  244. } else {
  245. permission.System = 0
  246. }
  247. if request.AdminPermission {
  248. permission.Admin = 1
  249. } else {
  250. permission.Admin = 0
  251. }
  252. if request.LogPermission {
  253. permission.Log = 1
  254. } else {
  255. permission.Log = 0
  256. }
  257. tx := a.mDB.Begin()
  258. defer common.DBTransaction(tx)
  259. //SECTION admin log
  260. log := Log{
  261. DB: tx,
  262. ActionType: "admin",
  263. SubActionType: "modified",
  264. Admin: admin,
  265. TargetID: request.AdminID,
  266. Target: "permission",
  267. }
  268. if err := tx.Save(&permission).Error; err != nil {
  269. gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err)
  270. tx.Rollback()
  271. return
  272. }
  273. if err = StackLog(log); err != nil {
  274. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.LogTrackingError, nil, err)
  275. tx.Rollback()
  276. return
  277. }
  278. if err := tx.Commit().Error; err != nil {
  279. gerror.IntegratedResponseToRequest(c, http.StatusInternalServerError, gerror.MysqlSaveError, nil, err)
  280. tx.Rollback()
  281. return
  282. }
  283. gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, nil, err)
  284. return
  285. }