system.users.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package main
  2. import (
  3. "net/http"
  4. "log"
  5. "strings"
  6. "encoding/json"
  7. "github.com/satori/go.uuid"
  8. auth "imuslab.com/arozos/mod/auth"
  9. )
  10. /*
  11. USERS MANAGER
  12. Manage user creation, listing, remove and others
  13. */
  14. func system_user_init(){
  15. http.HandleFunc("/system/users/list", system_user_handleList)
  16. http.HandleFunc("/system/users/editUser", system_user_handleUserEdit)
  17. http.HandleFunc("/system/users/userinfo", system_user_handleUserInfo)
  18. //Register setting interface for module configuration
  19. registerSetting(settingModule{
  20. Name: "My Account",
  21. Desc: "Manage your account and password",
  22. IconPath: "SystemAO/users/img/small_icon.png",
  23. Group: "Users",
  24. StartDir: "SystemAO/users/account.html",
  25. RequireAdmin: false,
  26. })
  27. registerSetting(settingModule{
  28. Name: "User List",
  29. Desc: "A list of users registered on this system",
  30. IconPath: "SystemAO/users/img/small_icon.png",
  31. Group: "Users",
  32. StartDir: "SystemAO/users/userList.html",
  33. RequireAdmin: true,
  34. })
  35. }
  36. //User edit handle. For admin to change settings for a user
  37. func system_user_handleUserEdit(w http.ResponseWriter, r *http.Request){
  38. //Require admin access
  39. if !system_permission_checkUserIsAdmin(w,r){
  40. sendErrorResponse(w, "Permission denied")
  41. }
  42. opr, _ := mv(r, "opr", true)
  43. username, _ := mv(r, "username", true)
  44. if !system_user_userExists(username){
  45. sendErrorResponse(w, "User not exists")
  46. return
  47. }
  48. if opr == ""{
  49. //List this user information
  50. type returnValue struct{
  51. Username string;
  52. Icondata string;
  53. Usergroup string;
  54. }
  55. iconData := getUserIcon(username)
  56. userGroup, err := system_permission_getUserGroups(username)
  57. if (err != nil){
  58. sendErrorResponse(w, "Unable to get user group")
  59. return;
  60. }
  61. jsonString, _ := json.Marshal(returnValue{
  62. Username: username,
  63. Icondata: iconData,
  64. Usergroup: userGroup,
  65. })
  66. sendJSONResponse(w, string(jsonString))
  67. }else if opr == "updateUserGroup"{
  68. //Update the target user's group
  69. newgroup, err := mv(r, "newgroup", true)
  70. if err != nil{
  71. sendErrorResponse(w, "New Group not defined");
  72. return
  73. }
  74. //Check if new group exists
  75. if !system_permission_groupExists(newgroup){
  76. sendErrorResponse(w, "Group not exists")
  77. return
  78. }
  79. //OK to proceed
  80. err = sysdb.Write("auth", "group/" + username, newgroup)
  81. if err != nil{
  82. sendErrorResponse(w, err.Error())
  83. return
  84. }
  85. sendOK(w)
  86. }else if opr == "resetPassword"{
  87. //Reset password for this user
  88. //Generate a random password for this user
  89. tmppassword := uuid.NewV4().String()
  90. hashedPassword := auth.Hash(tmppassword);
  91. err := sysdb.Write("auth", "passhash/" + username, hashedPassword)
  92. if err != nil{
  93. sendErrorResponse(w, err.Error())
  94. return
  95. }
  96. //Finish. Send back the reseted password
  97. sendJSONResponse(w, "\"" + tmppassword + "\"")
  98. }else{
  99. sendErrorResponse(w, "Not supported opr")
  100. return
  101. }
  102. }
  103. //User Info handler. Handle user's editing for his / her own profile
  104. func system_user_handleUserInfo(w http.ResponseWriter, r *http.Request){
  105. username, err := authAgent.GetUserName(w,r);
  106. if (err != nil){
  107. sendErrorResponse(w, "User not logged in")
  108. return;
  109. }
  110. opr, _ := mv(r, "opr", true)
  111. if (opr == ""){
  112. //Listing mode
  113. iconData := getUserIcon(username)
  114. userGroup, err := system_permission_getUserGroups(username)
  115. if (err != nil){
  116. sendErrorResponse(w, "Unable to get user group")
  117. return;
  118. }
  119. type returnValue struct{
  120. Username string;
  121. Icondata string;
  122. Usergroup string;
  123. }
  124. jsonString, _ := json.Marshal(returnValue{
  125. Username: username,
  126. Icondata: iconData,
  127. Usergroup: userGroup,
  128. })
  129. sendJSONResponse(w, string(jsonString))
  130. return;
  131. }else if (opr == "changepw"){
  132. oldpw, _ := mv(r, "oldpw", true)
  133. newpw, _ := mv(r, "newpw", true)
  134. if (oldpw == "" || newpw == ""){
  135. sendErrorResponse(w, "Password cannot be empty")
  136. return;
  137. }
  138. //valid the old password
  139. hashedPassword := auth.Hash(oldpw)
  140. var passwordInDB string
  141. err = sysdb.Read("auth", "passhash/" + username, &passwordInDB)
  142. if (hashedPassword != passwordInDB){
  143. //Old password entry invalid.
  144. sendErrorResponse(w, "Invalid old password.")
  145. return;
  146. }
  147. //OK! Change user password
  148. newHashedPassword := auth.Hash(newpw)
  149. sysdb.Write("auth", "passhash/" + username, newHashedPassword)
  150. sendOK(w);
  151. }else if (opr == "changeprofilepic"){
  152. picdata, _ := mv(r, "picdata", true)
  153. if (picdata != ""){
  154. setUserIcon(username, picdata);
  155. sendOK(w);
  156. }else{
  157. sendErrorResponse(w, "Empty image data received.")
  158. return
  159. }
  160. }else{
  161. sendErrorResponse(w, "Not supported opr")
  162. return
  163. }
  164. }
  165. //Get and set user profile icon
  166. func getUserIcon(username string) string{
  167. var userIconpath []byte;
  168. sysdb.Read("auth","profilepic/" + username, &userIconpath)
  169. return string(userIconpath);
  170. }
  171. func setUserIcon(username string, base64data string){
  172. sysdb.Write("auth","profilepic/" + username, []byte(base64data))
  173. return
  174. }
  175. func system_user_userExists(username string) bool{
  176. //Implement alternative interface for checking user exists
  177. return authAgent.UserExists(username);
  178. }
  179. func system_user_handleList(w http.ResponseWriter, r *http.Request){
  180. //List all users within the auth database.
  181. if (authAgent.CheckAuth(r) == false){
  182. //This user has not logged in
  183. sendErrorResponse(w, "User not logged in");
  184. return;
  185. }
  186. if (system_permission_checkUserIsAdmin(w,r) == true){
  187. entries,_ := sysdb.ListTable("auth")
  188. results := [][]string{}
  189. for _, keypairs := range entries{
  190. if (strings.Contains(string(keypairs[0]), "group/")){
  191. username:= strings.Split(string(keypairs[0]),"/")[1]
  192. group := ""
  193. //Get user icon if it exists in the database
  194. userIcon := getUserIcon(username)
  195. //Get the user account states
  196. accountStatus := "normal"
  197. sysdb.Read("auth","acstatus/" + username, &accountStatus)
  198. json.Unmarshal(keypairs[1], &group)
  199. results = append(results, []string{username, group, userIcon, accountStatus})
  200. }
  201. }
  202. jsonString, _ := json.Marshal(results)
  203. sendJSONResponse(w, string(jsonString))
  204. return
  205. }else{
  206. username, _ := authAgent.GetUserName(w,r);
  207. log.Println("[Permission] " + username + " tries to access admin only function.")
  208. sendErrorResponse(w, "Permission denied")
  209. return;
  210. }
  211. }