sso

login_service.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package service
  2. import (
  3. "SSO/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. func IsPasswordRight(mobile string, password string) bool {
  7. var count int
  8. readUserDb.Model(&models.AdminUser{}).Where("mobile = ? AND password = ? AND status = 1", mobile, password).Count(&count)
  9. return count == 1
  10. }
  11. func IsSuperAdmin(mobile string) bool {
  12. var count int
  13. readUserDb.Model(&models.AdminUser{}).Where("mobile = ? AND is_super_admin = 1 AND status = 1", mobile).Count(&count)
  14. return count == 1
  15. }
  16. func GetAdminUserWithMobile(mobile string) *models.AdminUser {
  17. var user models.AdminUser
  18. readUserDb.Where("mobile = ? AND status = 1", mobile).Find(&user)
  19. if user.Id == 0 {
  20. return nil
  21. } else {
  22. return &user
  23. }
  24. }
  25. func GetValidAdminUserByMobileReturnErr(mobile string) (*models.AdminUser, error) {
  26. var user models.AdminUser
  27. err := readUserDb.Where("mobile = ? AND status = 1", mobile).First(&user).Error
  28. if err != nil {
  29. if err == gorm.ErrRecordNotFound {
  30. return nil, nil
  31. } else {
  32. return nil, err
  33. }
  34. }
  35. return &user, nil
  36. }
  37. func DidAdminUserCreateOrg(adminUserID int) (bool, error) {
  38. var count int
  39. err := readUserDb.Model(&models.Org{}).Where("creator = ? AND status <> 0", adminUserID).Count(&count).Error
  40. return count > 0, err
  41. }
  42. func DidAdminUserOrgCreateApp(adminUserID int, appType int) (bool, error) {
  43. var count int
  44. db := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND status = 1", adminUserID)
  45. if appType != 0 {
  46. db = db.Where("app_type = ?", appType)
  47. }
  48. err := db.Count(&count).Error
  49. return count > 0, err
  50. }
  51. func GetAdminUserLastLoginLog(adminUserID int, appType int) (*models.AdminUserLoginLog, error) {
  52. var record models.AdminUserLoginLog
  53. err := readUserDb.Last(&record, "admin_user_id = ? AND operate_type <> 2 AND app_type = ?", adminUserID, appType).Error
  54. if err == gorm.ErrRecordNotFound {
  55. return nil, nil
  56. }
  57. return &record, err
  58. }
  59. func GetAdminUserPrioritizedAppType(adminUserID int) (int8, error) {
  60. rows, err := readUserDb.Raw("SELECT app.app_type FROM sgj_user_org_app AS app, sgj_user_admin_role AS r WHERE r.admin_user_id = ? AND r.app_id = app.id AND app.status = 1 AND r.status = 1 ORDER BY app.org_id ASC, app.app_type ASC LIMIT 1;", adminUserID).Rows()
  61. defer rows.Close()
  62. if err != nil {
  63. return 0, err
  64. } else {
  65. if rows.Next() {
  66. var appType int8
  67. rows.Scan(&appType)
  68. return appType, nil
  69. } else {
  70. return 0, nil
  71. }
  72. }
  73. }
  74. func GetAdminUserAllOrgWithUID(adminUserID int, isSuperAdmin bool) ([]*models.Org, error) {
  75. if isSuperAdmin {
  76. var org models.Org
  77. err := readUserDb.Preload("OrgGallery", "status = 1").Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
  78. if err != nil {
  79. return nil, err
  80. }
  81. return []*models.Org{&org}, nil
  82. } else {
  83. rows, err := readUserDb.Raw("SELECT org.*, COUNT(DISTINCT org.id) FROM sgj_user_admin_role AS u_r, sgj_user_org AS org WHERE u_r.admin_user_id = ? AND u_r.org_id = org.id AND u_r.status = 1 AND org.status <> 0 GROUP BY org.id ORDER BY org.id;", adminUserID).Rows()
  84. defer rows.Close()
  85. if err != nil {
  86. return nil, err
  87. } else {
  88. orgs := make([]*models.Org, 0)
  89. for rows.Next() {
  90. var org models.Org
  91. if scanErr := readUserDb.Preload("OrgGallery", "status = 1").ScanRows(rows, &org); scanErr != nil {
  92. return nil, scanErr
  93. } else {
  94. orgs = append(orgs, &org)
  95. }
  96. }
  97. return orgs, nil
  98. }
  99. }
  100. }
  101. func GetAdminUserAllOrgApp(adminUserID int, orgID int) ([]*models.OrgApp, error) {
  102. rows, err := readUserDb.Raw("SELECT app.* from sgj_user_admin_role AS u_r, sgj_user_org_app AS app WHERE u_r.admin_user_id = ? AND u_r.org_id = ? AND u_r.app_id = app.id AND u_r.status = 1 AND app.status = 1 ORDER BY app_type ASC;", adminUserID, orgID).Rows()
  103. defer rows.Close()
  104. if err != nil {
  105. return nil, err
  106. } else {
  107. apps := make([]*models.OrgApp, 0)
  108. for rows.Next() {
  109. var app models.OrgApp
  110. if scanErr := readUserDb.ScanRows(rows, &app); scanErr != nil {
  111. return nil, scanErr
  112. } else {
  113. apps = append(apps, &app)
  114. }
  115. }
  116. return apps, nil
  117. }
  118. }
  119. func InsertLoginLog(loginLog *models.AdminUserLoginLog) error {
  120. tx := writeUserDb.Begin()
  121. if err := tx.Create(loginLog).Error; err != nil {
  122. tx.Rollback()
  123. return err
  124. }
  125. return tx.Commit().Error
  126. }
  127. func GetAppRole(adminUserID int, orgID int, appID int) (*models.App_Role, error) {
  128. var appRole models.App_Role
  129. err := readUserDb.Where("admin_user_id = ? AND org_id = ? AND app_id = ? AND status = 1", adminUserID, orgID, appID).First(&appRole).Error
  130. if err != nil {
  131. return nil, err
  132. }
  133. return &appRole, nil
  134. }
  135. // 获取最近被创建的血透系统管理员角色(AppType为3)
  136. func GetLastXTAdminRole(adminUserID int, appType int) (*models.App_Role, error) {
  137. var appRole models.App_Role
  138. // select a_r.* from sgj_user_admin_role as a_r
  139. // join sgj_user_admin as a on a.id = a_r.admin_user_id
  140. // join sgj_user_org_app as app on app.id = a_r.app_id and app.status = 1 and app.app_type = 3
  141. // where a_r.status = 1 and a_r.admin_user_id = 380
  142. err := readUserDb.Table("sgj_user_admin_role").
  143. Select("sgj_user_admin_role.*").
  144. Joins("join sgj_user_admin as a on a.id = sgj_user_admin_role.admin_user_id").
  145. Joins("join sgj_user_org_app as app on app.id = sgj_user_admin_role.app_id and app.status = 1 and app.app_type = ?", appType).
  146. Where("sgj_user_admin_role.status = 1 and sgj_user_admin_role.admin_user_id = ?", adminUserID).
  147. Order("sgj_user_admin_role.id desc").
  148. First(&appRole).
  149. Error
  150. if err != nil {
  151. if err == gorm.ErrRecordNotFound {
  152. return nil, nil
  153. } else {
  154. return nil, err
  155. }
  156. }
  157. return &appRole, nil
  158. }
  159. func GetOrgById(orgID int) (*models.Org, error) {
  160. var org models.Org
  161. err := readUserDb.Model(&models.Org{}).Where("id = ?", orgID).First(&org).Error
  162. if err != nil {
  163. if err == gorm.ErrRecordNotFound {
  164. return nil, nil
  165. } else {
  166. return nil, err
  167. }
  168. }
  169. return &org, nil
  170. }
  171. func GetAppById(appID int) (*models.OrgApp, error) {
  172. var app models.OrgApp
  173. err := readUserDb.Model(&models.OrgApp{}).Where("id = ?", appID).First(&app).Error
  174. if err != nil {
  175. if err == gorm.ErrRecordNotFound {
  176. return nil, nil
  177. } else {
  178. return nil, err
  179. }
  180. }
  181. return &app, nil
  182. }