sso

login_service.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. var err error
  54. if appType > 0 {
  55. err = readUserDb.Last(&record, "admin_user_id = ? AND operate_type <> 2 AND app_type = ?", adminUserID, appType).Error
  56. } else {
  57. err = readUserDb.Last(&record, "admin_user_id = ? AND operate_type <> 2", adminUserID).Error
  58. }
  59. if err == gorm.ErrRecordNotFound {
  60. return nil, nil
  61. }
  62. return &record, err
  63. }
  64. func GetAdminUserPrioritizedAppType(adminUserID int) (int8, error) {
  65. 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()
  66. defer rows.Close()
  67. if err != nil {
  68. return 0, err
  69. } else {
  70. if rows.Next() {
  71. var appType int8
  72. rows.Scan(&appType)
  73. return appType, nil
  74. } else {
  75. return 0, nil
  76. }
  77. }
  78. }
  79. func GetAdminUserAllOrgWithUID(adminUserID int, isSuperAdmin bool) ([]*models.Org, error) {
  80. if isSuperAdmin {
  81. var org models.Org
  82. err := readUserDb.Preload("OrgGallery", "status = 1").Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
  83. if err != nil {
  84. return nil, err
  85. }
  86. return []*models.Org{&org}, nil
  87. } else {
  88. 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()
  89. defer rows.Close()
  90. if err != nil {
  91. return nil, err
  92. } else {
  93. orgs := make([]*models.Org, 0)
  94. for rows.Next() {
  95. var org models.Org
  96. if scanErr := readUserDb.Preload("OrgGallery", "status = 1").ScanRows(rows, &org); scanErr != nil {
  97. return nil, scanErr
  98. } else {
  99. orgs = append(orgs, &org)
  100. }
  101. }
  102. return orgs, nil
  103. }
  104. }
  105. }
  106. func GetAdminUserAllOrgApp(adminUserID int, orgID int) ([]*models.OrgApp, error) {
  107. 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()
  108. defer rows.Close()
  109. if err != nil {
  110. return nil, err
  111. } else {
  112. apps := make([]*models.OrgApp, 0)
  113. for rows.Next() {
  114. var app models.OrgApp
  115. if scanErr := readUserDb.ScanRows(rows, &app); scanErr != nil {
  116. return nil, scanErr
  117. } else {
  118. apps = append(apps, &app)
  119. }
  120. }
  121. return apps, nil
  122. }
  123. }
  124. func InsertLoginLog(loginLog *models.AdminUserLoginLog) error {
  125. tx := writeUserDb.Begin()
  126. if err := tx.Create(loginLog).Error; err != nil {
  127. tx.Rollback()
  128. return err
  129. }
  130. return tx.Commit().Error
  131. }
  132. func GetAppRole(adminUserID int, orgID int, appID int) (*models.App_Role, error) {
  133. var appRole models.App_Role
  134. err := readUserDb.Where("admin_user_id = ? AND org_id = ? AND app_id = ? AND status = 1", adminUserID, orgID, appID).First(&appRole).Error
  135. if err != nil {
  136. return nil, err
  137. }
  138. return &appRole, nil
  139. }
  140. // 获取最近被创建的血透系统管理员角色(AppType为3)
  141. func GetLastXTAdminRole(adminUserID int, appType int) (*models.App_Role, error) {
  142. var appRole models.App_Role
  143. // select a_r.* from sgj_user_admin_role as a_r
  144. // join sgj_user_admin as a on a.id = a_r.admin_user_id
  145. // join sgj_user_org_app as app on app.id = a_r.app_id and app.status = 1 and app.app_type = 3
  146. // where a_r.status = 1 and a_r.admin_user_id = 380
  147. err := readUserDb.Table("sgj_user_admin_role").
  148. Select("sgj_user_admin_role.*").
  149. Joins("join sgj_user_admin as a on a.id = sgj_user_admin_role.admin_user_id").
  150. 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).
  151. Where("sgj_user_admin_role.status = 1 and sgj_user_admin_role.admin_user_id = ?", adminUserID).
  152. Order("sgj_user_admin_role.id desc").
  153. First(&appRole).
  154. Error
  155. if err != nil {
  156. if err == gorm.ErrRecordNotFound {
  157. return nil, nil
  158. } else {
  159. return nil, err
  160. }
  161. }
  162. return &appRole, nil
  163. }
  164. func GetOrgById(orgID int) (*models.Org, error) {
  165. var org models.Org
  166. err := readUserDb.Model(&models.Org{}).Where("id = ?", orgID).First(&org).Error
  167. if err != nil {
  168. if err == gorm.ErrRecordNotFound {
  169. return nil, nil
  170. } else {
  171. return nil, err
  172. }
  173. }
  174. return &org, nil
  175. }
  176. func GetAppById(appID int) (*models.OrgApp, error) {
  177. var app models.OrgApp
  178. err := readUserDb.Model(&models.OrgApp{}).Where("id = ?", appID).First(&app).Error
  179. if err != nil {
  180. if err == gorm.ErrRecordNotFound {
  181. return nil, nil
  182. } else {
  183. return nil, err
  184. }
  185. }
  186. return &app, nil
  187. }