permissionHandler.go 6.4 KB

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