user.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package user
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "os"
  7. "golang.org/x/sync/syncmap"
  8. auth "imuslab.com/arozos/mod/auth"
  9. db "imuslab.com/arozos/mod/database"
  10. permission "imuslab.com/arozos/mod/permission"
  11. quota "imuslab.com/arozos/mod/quota"
  12. storage "imuslab.com/arozos/mod/storage"
  13. )
  14. var (
  15. //Create a buffer to put the pointers to created user quota managers, mapped by username
  16. //quotaManagerBuffer map[string]*quota.QuotaHandler = map[string]*quota.QuotaHandler{}
  17. quotaManagerBuffer = syncmap.Map{}
  18. )
  19. type User struct {
  20. Username string
  21. StorageQuota *quota.QuotaHandler
  22. PermissionGroup []*permission.PermissionGroup
  23. HomeDirectories *storage.StoragePool
  24. parent *UserHandler
  25. }
  26. type UserHandler struct {
  27. authAgent *auth.AuthAgent
  28. database *db.Database
  29. phandler *permission.PermissionHandler
  30. basePool *storage.StoragePool
  31. }
  32. //Initiate a new user handler
  33. func NewUserHandler(systemdb *db.Database, authAgent *auth.AuthAgent, permissionHandler *permission.PermissionHandler, baseStoragePool *storage.StoragePool) (*UserHandler, error) {
  34. return &UserHandler{
  35. authAgent: authAgent,
  36. database: systemdb,
  37. phandler: permissionHandler,
  38. basePool: baseStoragePool,
  39. }, nil
  40. }
  41. //Return the user handler's auth agent
  42. func (u *UserHandler) GetAuthAgent() *auth.AuthAgent {
  43. return u.authAgent
  44. }
  45. func (u *UserHandler) GetPermissionHandler() *permission.PermissionHandler {
  46. return u.phandler
  47. }
  48. func (u *UserHandler) GetStoragePool() *storage.StoragePool {
  49. return u.basePool
  50. }
  51. func (u *UserHandler) GetDatabase() *db.Database {
  52. return u.database
  53. }
  54. func (u *UserHandler) UpdateStoragePool(newpool *storage.StoragePool) {
  55. u.basePool = newpool
  56. }
  57. //Get User object from username
  58. func (u *UserHandler) GetUserInfoFromUsername(username string) (*User, error) {
  59. //Check if user exists
  60. if !u.authAgent.UserExists(username) {
  61. return &User{}, errors.New("User not exists")
  62. }
  63. //Get the user's permission group
  64. permissionGroups, err := u.phandler.GetUsersPermissionGroup(username)
  65. if err != nil {
  66. return &User{}, err
  67. }
  68. //Create user directories in the Home Directories
  69. if u.basePool.Storages == nil {
  70. //This userhandler do not have a basepool?
  71. log.Println("USER HANDLER DO NOT HAVE BASEPOOL")
  72. } else {
  73. for _, store := range u.basePool.Storages {
  74. if store.Hierarchy == "user" {
  75. os.MkdirAll(store.Path+"/users/"+username, 0755)
  76. }
  77. }
  78. }
  79. thisUser := User{
  80. Username: username,
  81. PermissionGroup: permissionGroups,
  82. HomeDirectories: u.basePool,
  83. parent: u,
  84. }
  85. //Get the storage quota manager for thus user
  86. var thisUserQuotaManager *quota.QuotaHandler
  87. if val, ok := quotaManagerBuffer.Load(username); ok {
  88. //user quota manager exists
  89. thisUserQuotaManager = val.(*quota.QuotaHandler)
  90. } else {
  91. //Get the largest quota from the user's group
  92. maxQuota := int64(0)
  93. for _, group := range permissionGroups {
  94. if group.DefaultStorageQuota == -1 {
  95. //Admin
  96. maxQuota = -1
  97. break
  98. } else if group.DefaultStorageQuota > maxQuota {
  99. //Other groups. Get the largest one
  100. maxQuota = group.DefaultStorageQuota
  101. }
  102. }
  103. //Create a new manager for this user
  104. allFsHandlers := thisUser.GetAllFileSystemHandler()
  105. thisUserQuotaManager = quota.NewUserQuotaHandler(u.database, username, allFsHandlers, maxQuota)
  106. if !thisUserQuotaManager.IsQuotaInitialized() {
  107. //This user quota hasn't been initalized. Initalize it now to match its group
  108. userMaxDefaultStorageQuota := permission.GetLargestStorageQuotaFromGroups(permissionGroups)
  109. thisUserQuotaManager.SetUserStorageQuota(userMaxDefaultStorageQuota)
  110. }
  111. //Push the manger to buffer
  112. quotaManagerBuffer.Store(username, thisUserQuotaManager)
  113. }
  114. thisUser.StorageQuota = thisUserQuotaManager
  115. //Return the user object
  116. return &thisUser, nil
  117. }
  118. //Get user obejct from session
  119. func (u *UserHandler) GetUserInfoFromRequest(w http.ResponseWriter, r *http.Request) (*User, error) {
  120. username, err := u.authAgent.GetUserName(w, r)
  121. if err != nil {
  122. return &User{}, err
  123. }
  124. userObject, err := u.GetUserInfoFromUsername(username)
  125. if err != nil {
  126. return &User{}, err
  127. }
  128. return userObject, nil
  129. }