permissionHandler.go 6.3 KB

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