permissionHandler.go 6.7 KB

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