localusers.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "time"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/tristate"
  22. "yunion.io/x/sqlchemy"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. )
  25. // +onecloud:swagger-gen-ignore
  26. type SLocalUserManager struct {
  27. db.SResourceBaseManager
  28. }
  29. var LocalUserManager *SLocalUserManager
  30. func init() {
  31. LocalUserManager = &SLocalUserManager{
  32. SResourceBaseManager: db.NewResourceBaseManager(
  33. SLocalUser{},
  34. "local_user",
  35. "local_user",
  36. "local_users",
  37. ),
  38. }
  39. LocalUserManager.SetVirtualObject(LocalUserManager)
  40. }
  41. /*
  42. +-------------------+--------------+------+-----+---------+----------------+
  43. | Field | Type | Null | Key | Default | Extra |
  44. +-------------------+--------------+------+-----+---------+----------------+
  45. | id | int(11) | NO | PRI | NULL | auto_increment |
  46. | user_id | varchar(64) | NO | UNI | NULL | |
  47. | domain_id | varchar(64) | NO | MUL | NULL | |
  48. | name | varchar(255) | NO | | NULL | |
  49. | failed_auth_count | int(11) | YES | | NULL | |
  50. | failed_auth_at | datetime | YES | | NULL | |
  51. +-------------------+--------------+------+-----+---------+----------------+
  52. */
  53. type SLocalUser struct {
  54. db.SResourceBase
  55. Id int `nullable:"false" primary:"true" auto_increment:"true"`
  56. UserId string `width:"64" charset:"ascii" nullable:"false" index:"true"`
  57. DomainId string `width:"64" charset:"ascii" nullable:"false" index:"true"`
  58. Name string `width:"255" charset:"utf8" nullable:"false"`
  59. FailedAuthCount int `nullable:"true"`
  60. FailedAuthAt time.Time `nullable:"true"`
  61. NeedResetPassword tristate.TriState `default:"false" list:"domain"`
  62. ResetHint string `width:"16" charset:"ascii" list:"domain"`
  63. }
  64. func (user *SLocalUser) GetId() string {
  65. return fmt.Sprintf("%d", user.Id)
  66. }
  67. func (user *SLocalUser) GetName() string {
  68. return user.Name
  69. }
  70. func (manager *SLocalUserManager) CreateByInsertOrUpdate() bool {
  71. return false
  72. }
  73. func (manager *SLocalUserManager) FetchLocalUserById(localId int) (*SLocalUser, error) {
  74. return manager.fetchLocalUser("", "", localId)
  75. }
  76. func (manager *SLocalUserManager) fetchLocalUser(userId string, domainId string, localId int) (*SLocalUser, error) {
  77. localUser := SLocalUser{}
  78. localUser.SetModelManager(manager, &localUser)
  79. var q *sqlchemy.SQuery
  80. if len(userId) > 0 && len(domainId) > 0 {
  81. q = manager.Query().Equals("user_id", userId).Equals("domain_id", domainId)
  82. } else {
  83. q = manager.Query().Equals("id", localId)
  84. }
  85. err := q.First(&localUser)
  86. if err != nil {
  87. if err == sql.ErrNoRows {
  88. return nil, err
  89. } else {
  90. return nil, errors.Wrap(err, "Query")
  91. }
  92. }
  93. return &localUser, nil
  94. }
  95. func (manager *SLocalUserManager) register(userId string, domainId string, name string) (*SLocalUser, error) {
  96. localUser, err := manager.fetchLocalUser(userId, domainId, 0)
  97. if err != nil && err != sql.ErrNoRows {
  98. return nil, errors.Wrap(err, "Query")
  99. }
  100. if err == nil {
  101. return localUser, nil
  102. }
  103. localUser = &SLocalUser{}
  104. localUser.SetModelManager(manager, localUser)
  105. localUser.UserId = userId
  106. localUser.DomainId = domainId
  107. localUser.Name = name
  108. err = manager.TableSpec().Insert(context.TODO(), localUser)
  109. if err != nil {
  110. return nil, errors.Wrap(err, "Insert")
  111. }
  112. return localUser, nil
  113. }
  114. func (manager *SLocalUserManager) delete(userId string, domainId string) (*SLocalUser, error) {
  115. localUser, err := manager.fetchLocalUser(userId, domainId, 0)
  116. if err != nil && err != sql.ErrNoRows {
  117. return nil, errors.Wrap(err, "Query")
  118. }
  119. if err == sql.ErrNoRows {
  120. return nil, nil
  121. }
  122. _, err = db.Update(localUser, func() error {
  123. return localUser.MarkDelete()
  124. })
  125. if err != nil {
  126. return nil, errors.Wrap(err, "MarkDelete")
  127. }
  128. return localUser, nil
  129. }
  130. func (usr *SLocalUser) SaveFailedAuth() error {
  131. _, err := db.Update(usr, func() error {
  132. usr.FailedAuthCount += 1
  133. usr.FailedAuthAt = time.Now()
  134. return nil
  135. })
  136. if err != nil {
  137. return errors.Wrap(err, "Update")
  138. }
  139. return nil
  140. }
  141. func (usr *SLocalUser) ClearFailedAuth() error {
  142. _, err := db.Update(usr, func() error {
  143. usr.FailedAuthCount = 0
  144. usr.FailedAuthAt = time.Time{}
  145. return nil
  146. })
  147. if err != nil {
  148. return errors.Wrap(err, "Update")
  149. }
  150. return nil
  151. }
  152. func (usr *SLocalUser) markNeedResetPassword(needReset bool, reason string) error {
  153. if usr.NeedResetPassword.IsTrue() == needReset {
  154. return nil
  155. }
  156. _, err := db.Update(usr, func() error {
  157. if needReset {
  158. usr.NeedResetPassword = tristate.True
  159. usr.ResetHint = reason
  160. } else {
  161. usr.NeedResetPassword = tristate.False
  162. usr.ResetHint = reason
  163. }
  164. return nil
  165. })
  166. if err != nil {
  167. return errors.Wrap(err, "Update")
  168. }
  169. return nil
  170. }