system.permission.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "strings"
  7. //"log"
  8. )
  9. /*
  10. This is the permission management module of the ArOZ Online System
  11. In default mode, the system only contains a user group named "administrator"
  12. This module also handle permission checking and others
  13. */
  14. //Initiate function for system permission
  15. func system_permission_service_init() {
  16. //Register permission configuration endpoints
  17. http.HandleFunc("/system/permission/listgroup", system_permission_handleListGroup)
  18. http.HandleFunc("/system/permission/newgroup", system_permission_createUserGroupHandler)
  19. http.HandleFunc("/system/permission/delgroup", system_permission_removeUserGroupHandler)
  20. http.HandleFunc("/system/permission/isAdmin", system_permission_handleAdminCheck)
  21. //http.HandleFunc("/system/permission/groupdetails", system_permission_handleGroupDetail)
  22. //Create table if not exists
  23. sysdb.NewTable("permission")
  24. //Register setting interface for module configuration
  25. registerSetting(settingModule{
  26. Name: "Permission Groups",
  27. Desc: "Handle the permission of access in groups",
  28. IconPath: "SystemAO/users/img/small_icon.png",
  29. Group: "Users",
  30. StartDir: "SystemAO/users/group.html",
  31. RequireAdmin: true,
  32. })
  33. }
  34. func system_permission_getUserGroups(username string) (string, error) {
  35. group := ""
  36. sysdb.Read( "auth", "group/"+username, &group)
  37. if group == "" {
  38. return "", errors.New("User group not found")
  39. }
  40. return group, nil
  41. }
  42. //Return a list of usergorup stored in the system
  43. func system_permission_listGroup() []string {
  44. groups := []string{}
  45. entries, _ := sysdb.ListTable("permission")
  46. for _, keypairs := range entries {
  47. if strings.Contains(string(keypairs[0]), "group/") {
  48. groups = append(groups, strings.Split(string(keypairs[0]), "/")[1])
  49. }
  50. }
  51. return groups
  52. }
  53. //Return the current list of usergroup from the database as JSON
  54. func system_permission_handleListGroup(w http.ResponseWriter, r *http.Request) {
  55. groups := []string{}
  56. entries, _ := sysdb.ListTable("permission")
  57. listPermission, _ := mv(r, "showper", false)
  58. if listPermission == "" {
  59. for _, keypairs := range entries {
  60. if strings.Contains(string(keypairs[0]), "group/") {
  61. //This is a group name record. Append the group name only.
  62. groups = append(groups, strings.Split(string(keypairs[0]), "/")[1])
  63. }
  64. }
  65. //Check if the group list is empty. If yes, create the administrator group
  66. if len(groups) == 0 {
  67. err := system_permission_createGroup("administrator", []string{"*"})
  68. if err != nil {
  69. panic("Failed to create administrator group. Is database writable?")
  70. }
  71. groups = append(groups, "administrator")
  72. }
  73. jsonString, _ := json.Marshal(groups)
  74. sendJSONResponse(w, string(jsonString))
  75. } else {
  76. results := map[string][]string{}
  77. for _, keypairs := range entries {
  78. if strings.Contains(string(keypairs[0]), "group/") {
  79. //This is a group name record. Append the group name only.
  80. thisGroupPermission := []string{}
  81. json.Unmarshal(keypairs[1], &thisGroupPermission)
  82. results[strings.Split(string(keypairs[0]), "/")[1]] = thisGroupPermission
  83. }
  84. }
  85. jsonString, _ := json.Marshal(results)
  86. sendJSONResponse(w, string(jsonString))
  87. }
  88. /*
  89. //Deprecated method for listing group with JSON string storage
  90. groupsData := "";
  91. sysdb.Read( "permission", "groups", &groups)
  92. //There are always at least one group and this key must be valid. Not need to check for error.
  93. w.Header().Set("Content-Type", "application/json")
  94. sendTextResponse(w,groups)
  95. */
  96. }
  97. func system_permission_handleAdminCheck(w http.ResponseWriter, r *http.Request) {
  98. isAdmin := system_permission_checkUserIsAdmin(w, r)
  99. if isAdmin {
  100. sendJSONResponse(w, "true")
  101. } else {
  102. sendJSONResponse(w, "false")
  103. }
  104. }
  105. func system_permission_handleGroupDetail(w http.ResponseWriter, r *http.Request) {
  106. opr, _ := mv(r, "opr", false)
  107. if opr == "" {
  108. //List all groups with detail
  109. var groupsRaw []byte
  110. sysdb.Read( "permission", "groups", &groupsRaw)
  111. var groups []string
  112. json.Unmarshal(groupsRaw, &groups)
  113. }
  114. }
  115. func system_permission_createGroup(groupname string, modulepermission []string) error {
  116. //Check if group already exists
  117. if !system_permission_groupExists(groupname) {
  118. //This group do not exists. Continue to create
  119. err := sysdb.Write("permission", "group/"+groupname, modulepermission)
  120. if err != nil {
  121. return err
  122. }
  123. } else {
  124. //This group exists.
  125. return errors.New("Group already exists")
  126. }
  127. return nil
  128. }
  129. func system_permission_removeUserGroupHandler(w http.ResponseWriter, r *http.Request) {
  130. //Check if user is admin
  131. isAdmin := system_permission_checkUserIsAdmin(w, r)
  132. if !isAdmin {
  133. sendErrorResponse(w, "Permission denied")
  134. return
  135. }
  136. //Check if the groupname is provided
  137. groupname, err := mv(r, "groupname", false)
  138. if err != nil {
  139. sendErrorResponse(w, "Groupname not defined")
  140. return
  141. }
  142. //Remove the group from database
  143. if system_permission_groupExists(groupname) {
  144. //This group exits. Continue removal
  145. err := sysdb.Delete("permission", "group/"+groupname)
  146. if err != nil {
  147. sendErrorResponse(w, err.Error())
  148. return
  149. }
  150. } else {
  151. //This group exists.
  152. sendErrorResponse(w, "Given group not exists")
  153. return
  154. }
  155. sendOK(w)
  156. }
  157. func system_permission_createUserGroupHandler(w http.ResponseWriter, r *http.Request) {
  158. isAdmin := system_permission_checkUserIsAdmin(w, r)
  159. if !isAdmin {
  160. sendErrorResponse(w, "Permission denied")
  161. return
  162. }
  163. groupname, err := mv(r, "groupname", true)
  164. if err != nil {
  165. sendErrorResponse(w, "Groupname not defined")
  166. return
  167. }
  168. permissions, err := mv(r, "permission", true)
  169. if err != nil {
  170. sendErrorResponse(w, "Permission not defined")
  171. return
  172. }
  173. permissionList := []string{}
  174. err = json.Unmarshal([]byte(permissions), &permissionList)
  175. if err != nil {
  176. sendErrorResponse(w, "Failed to parse the permission list")
  177. return
  178. }
  179. system_permission_createGroup(groupname, permissionList)
  180. sendOK(w)
  181. }
  182. func system_permission_getGroupAccessList(groupname string) []string {
  183. moduleList := []string{}
  184. err := sysdb.Read( "permission", "group/"+groupname, &moduleList)
  185. if err != nil {
  186. return []string{}
  187. }
  188. return moduleList
  189. }
  190. func system_permission_checkUserHasAccessToModule(username string, modulename string) bool {
  191. //Get user group and see if group exists.
  192. usergroup := system_permission_getUserPermissionGroup(username)
  193. groupExists := system_permission_groupExists(usergroup)
  194. if !groupExists {
  195. return false
  196. }
  197. //Group exists. Check permission on module
  198. groupAccessList := system_permission_getGroupAccessList(usergroup)
  199. if len(groupAccessList) == 1 && groupAccessList[0] == "*" {
  200. return true
  201. }
  202. if stringInSlice(modulename, groupAccessList) {
  203. return true
  204. }
  205. return false
  206. }
  207. func system_permission_checkUserIsAdmin(w http.ResponseWriter, r *http.Request) bool {
  208. username, err := authAgent.GetUserName(w, r)
  209. if err != nil{
  210. return false
  211. }
  212. userGroup := system_permission_getUserPermissionGroup(username)
  213. if userGroup == "administrator" {
  214. return true
  215. }
  216. return false
  217. }
  218. func system_permission_getUserPermissionGroup(username string) string {
  219. usergroup := ""
  220. sysdb.Read( "auth", "group/"+username, &usergroup)
  221. return (usergroup)
  222. }
  223. //This function check if the given usergroup ID exists.
  224. func system_permission_groupExists(group string) bool {
  225. dummyPermission := []string{}
  226. err := sysdb.Read( "permission", "group/"+group, &dummyPermission)
  227. if err != nil || len(dummyPermission) == 0 {
  228. return false
  229. }
  230. return true
  231. }