permissionHandler.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package user
  2. import (
  3. //"log"
  4. "errors"
  5. "path/filepath"
  6. "strings"
  7. permission "imuslab.com/arozos/mod/permission"
  8. storage "imuslab.com/arozos/mod/storage"
  9. )
  10. //Permissions related to modules
  11. func (u *User) GetModuleAccessPermission(moduleName string) bool {
  12. //Check if this module permission is within user's permission group access
  13. moduleName = strings.ToLower(moduleName)
  14. for _, pg := range u.PermissionGroup {
  15. if pg.IsAdmin == true {
  16. //This user is admin. Allow all module access
  17. return true
  18. } else if inSliceIgnoreCase(pg.AccessibleModules, moduleName) {
  19. //This permission group contain the module we want. Allow accessed
  20. return true
  21. } else if moduleName == strings.ToLower(pg.DefaultInterfaceModule) {
  22. //This is the interfacing module for the group this user is in
  23. return true
  24. }
  25. }
  26. //This user has no permission group that has access to this module
  27. return false
  28. }
  29. func (u *User) GetUserAccessibleModules() []string {
  30. userAccessibleModules := []string{}
  31. for _, pg := range u.GetUserPermissionGroup() {
  32. groupAccessiableModules := pg.AccessibleModules
  33. for _, gmod := range groupAccessiableModules {
  34. userAccessibleModules = append(userAccessibleModules, gmod)
  35. }
  36. }
  37. return userAccessibleModules
  38. }
  39. func (u *User) IsAdmin() bool {
  40. isAdmin := false
  41. for _, pg := range u.PermissionGroup {
  42. if pg.IsAdmin == true {
  43. isAdmin = true
  44. }
  45. }
  46. return isAdmin
  47. }
  48. //Get the (or a list of ) Interface Module (aka booting module) for this user, returning module uuids
  49. func (u *User) GetInterfaceModules() []string {
  50. results := []string{}
  51. for _, pg := range u.PermissionGroup {
  52. if !inSlice(results, pg.DefaultInterfaceModule) {
  53. results = append(results, pg.DefaultInterfaceModule)
  54. }
  55. }
  56. return results
  57. }
  58. //Check if the user has access to this virthal filepath
  59. func (u *User) GetPathAccessPermission(vpath string) string {
  60. fsid, _, err := getIDFromVirtualPath(filepath.ToSlash(vpath))
  61. if err != nil {
  62. return "denied"
  63. }
  64. topAccessRightStoragePool, err := u.GetHighestAccessRightStoragePool(fsid)
  65. if err != nil {
  66. return "denied"
  67. }
  68. if topAccessRightStoragePool.Owner == u.Username {
  69. //This user own this storage pool. CHeck if the fs itself is readonly
  70. fsHandler, _ := getHandlerFromID(u.GetAllFileSystemHandler(), fsid)
  71. if fsHandler.ReadOnly {
  72. return "readonly"
  73. }
  74. return "readwrite"
  75. } else if topAccessRightStoragePool.Owner == "system" {
  76. //System storage pool. Allow both read and write if the system handler is readwrite
  77. fsHandler, _ := getHandlerFromID(u.GetAllFileSystemHandler(), fsid)
  78. if fsHandler.ReadOnly {
  79. return "readonly"
  80. }
  81. return "readwrite"
  82. } else {
  83. //This user do not own this storage pool. Use the pools' config
  84. fsHandler, _ := getHandlerFromID(u.GetAllFileSystemHandler(), fsid)
  85. if fsHandler.ReadOnly {
  86. return "readonly"
  87. }
  88. return topAccessRightStoragePool.OtherPermission
  89. }
  90. }
  91. //Helper function for checking permission
  92. func (u *User) CanRead(vpath string) bool {
  93. rwp := u.GetPathAccessPermission(vpath)
  94. if rwp == "readonly" || rwp == "readwrite" {
  95. return true
  96. } else {
  97. return false
  98. }
  99. }
  100. func (u *User) CanWrite(vpath string) bool {
  101. rwp := u.GetPathAccessPermission(vpath)
  102. if rwp == "readwrite" {
  103. return true
  104. } else {
  105. return false
  106. }
  107. }
  108. //Get the highest access right to the given fs uuid
  109. func (u *User) GetHighestAccessRightStoragePool(fsUUID string) (*storage.StoragePool, error) {
  110. //List all storage pool that have access to this fsUUID
  111. matchingStoragePool := []*storage.StoragePool{}
  112. for _, h := range u.HomeDirectories.Storages {
  113. if h.UUID == fsUUID {
  114. //User Home directory contain access to this fsUUID
  115. matchingStoragePool = append(matchingStoragePool, u.HomeDirectories)
  116. }
  117. }
  118. //Look for other permission groups this user is in
  119. for _, pg := range u.PermissionGroup {
  120. for _, h := range pg.StoragePool.Storages {
  121. if h.UUID == fsUUID {
  122. //User Home directory contain access to this fsUUID
  123. matchingStoragePool = append(matchingStoragePool, u.HomeDirectories)
  124. }
  125. }
  126. }
  127. //Check the highest priority in the list
  128. if len(matchingStoragePool) == 0 {
  129. return &storage.StoragePool{}, errors.New("No access to this filesystem was found")
  130. }
  131. currentTopStoragePool := matchingStoragePool[0]
  132. for _, storagePool := range matchingStoragePool {
  133. if storagePool.Owner == u.Username {
  134. //Owner of this ppol. Return this
  135. return storagePool, nil
  136. } else if storagePool.Owner == "system" {
  137. //System storage pool. Everyone can read write to this.
  138. return storagePool, nil
  139. } else {
  140. //Compare the priority. Replace the top one if the current one has higher priority
  141. if storagePool.HasHigherOrEqualPermissionThan(currentTopStoragePool) {
  142. currentTopStoragePool = storagePool
  143. }
  144. }
  145. }
  146. return currentTopStoragePool, nil
  147. }
  148. func (u *User) GetUserPermissionGroup() []*permission.PermissionGroup {
  149. return u.PermissionGroup
  150. }
  151. //Check if the user is in one of the permission groups, require groupname
  152. func (u *User) UserIsInOneOfTheGroupOf(groupnames []string) bool {
  153. userpg := u.GetUserPermissionGroup()
  154. for _, thispg := range userpg {
  155. for _, thisname := range groupnames {
  156. if thispg.Name == thisname {
  157. return true
  158. }
  159. }
  160. }
  161. return false
  162. }
  163. func (u *User) SetUserPermissionGroup(groups []*permission.PermissionGroup) {
  164. groupIds := []string{}
  165. for _, gp := range groups {
  166. groupIds = append(groupIds, gp.Name)
  167. }
  168. u.parent.database.Write("auth", "group/"+u.Username, groupIds)
  169. }