user_login.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "time"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. // +onecloud:swagger-gen-ignore
  25. type SUserLoginManager struct {
  26. db.SModelBaseManager
  27. }
  28. var UserLoginManager *SUserLoginManager
  29. func init() {
  30. UserLoginManager = &SUserLoginManager{
  31. SModelBaseManager: db.NewModelBaseManager(
  32. SUserLogin{},
  33. "user_login",
  34. "user_login",
  35. "user_logins",
  36. ),
  37. }
  38. UserLoginManager.SetVirtualObject(UserLoginManager)
  39. }
  40. // +onecloud:swagger-gen-ignore
  41. type SUserLogin struct {
  42. db.SModelBase
  43. UserId string `width:"64" charset:"ascii" nullable:"false" primary:"true"`
  44. // 上次登录时间
  45. LastActiveAt time.Time `nullable:"true" list:"domain"`
  46. // 上次用户登录IP
  47. LastLoginIp string `nullable:"true" list:"domain"`
  48. // 上次用户登录方式,可能值有:web(web控制台),cli(命令行climc),API(api)
  49. LastLoginSource string `nullable:"true" list:"domain"`
  50. }
  51. func (manager *SUserLoginManager) fetchUserLogin(userId string) (*SUserLogin, error) {
  52. userLogin := &SUserLogin{}
  53. userLogin.SetModelManager(manager, userLogin)
  54. err := manager.Query().Equals("user_id", userId).First(userLogin)
  55. if err != nil {
  56. return nil, errors.Wrap(err, "Query")
  57. }
  58. return userLogin, nil
  59. }
  60. func (manager *SUserLoginManager) traceLoginEvent(ctx context.Context, userId string, authCtx mcclient.SAuthContext) error {
  61. userLogin, err := manager.fetchUserLogin(userId)
  62. if err != nil {
  63. if errors.Cause(err) == sql.ErrNoRows {
  64. // do insert
  65. userLogin := &SUserLogin{
  66. UserId: userId,
  67. LastActiveAt: time.Now().UTC(),
  68. LastLoginIp: authCtx.Ip,
  69. LastLoginSource: authCtx.Source,
  70. }
  71. err := manager.TableSpec().Insert(ctx, userLogin)
  72. if err != nil {
  73. return errors.Wrap(err, "Insert")
  74. }
  75. return nil
  76. } else {
  77. return errors.Wrap(err, "fetchUserLogin")
  78. }
  79. }
  80. // only save web console login record
  81. if userLogin.LastActiveAt.IsZero() || utils.IsInArray(authCtx.Source, []string{mcclient.AuthSourceWeb}) {
  82. _, err := db.Update(userLogin, func() error {
  83. userLogin.LastActiveAt = time.Now().UTC()
  84. userLogin.LastLoginIp = authCtx.Ip
  85. userLogin.LastLoginSource = authCtx.Source
  86. return nil
  87. })
  88. if err != nil {
  89. return errors.Wrap(err, "Update")
  90. }
  91. }
  92. return nil
  93. }