userFunc.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package agi
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "net/http"
  7. "path/filepath"
  8. "github.com/robertkrimen/otto"
  9. user "imuslab.com/arozos/mod/user"
  10. )
  11. //Define path translation function
  12. func virtualPathToRealPath(path string, u *user.User) (string, error) {
  13. return u.VirtualPathToRealPath(path)
  14. }
  15. func realpathToVirtualpath(path string, u *user.User) (string, error) {
  16. return u.RealPathToVirtualPath(path)
  17. }
  18. //Inject user based functions into the virtual machine
  19. func (g *Gateway) injectUserFunctions(vm *otto.Otto, u *user.User, w http.ResponseWriter, r *http.Request) {
  20. username := u.Username
  21. vm.Set("USERNAME", username)
  22. vm.Set("USERICON", u.GetUserIcon())
  23. vm.Set("USERQUOTA_TOTAL", u.StorageQuota.TotalStorageQuota)
  24. vm.Set("USERQUOTA_USED", u.StorageQuota.UsedStorageQuota)
  25. vm.Set("USER_VROOTS", u.GetAllFileSystemHandler())
  26. vm.Set("USER_MODULES", u.GetUserAccessibleModules())
  27. //File system and path related
  28. vm.Set("decodeVirtualPath", func(call otto.FunctionCall) otto.Value {
  29. path, _ := call.Argument(0).ToString()
  30. realpath, err := virtualPathToRealPath(path, u)
  31. if err != nil {
  32. reply, _ := vm.ToValue(false)
  33. return reply
  34. } else {
  35. reply, _ := vm.ToValue(realpath)
  36. return reply
  37. }
  38. })
  39. vm.Set("decodeAbsoluteVirtualPath", func(call otto.FunctionCall) otto.Value {
  40. path, _ := call.Argument(0).ToString()
  41. realpath, err := virtualPathToRealPath(path, u)
  42. if err != nil {
  43. reply, _ := vm.ToValue(false)
  44. return reply
  45. } else {
  46. //Convert the real path to absolute path
  47. abspath, err := filepath.Abs(realpath)
  48. if err != nil {
  49. reply, _ := vm.ToValue(false)
  50. return reply
  51. }
  52. reply, _ := vm.ToValue(abspath)
  53. return reply
  54. }
  55. })
  56. vm.Set("encodeRealPath", func(call otto.FunctionCall) otto.Value {
  57. path, _ := call.Argument(0).ToString()
  58. realpath, err := realpathToVirtualpath(path, u)
  59. if err != nil {
  60. reply, _ := vm.ToValue(false)
  61. return reply
  62. } else {
  63. reply, _ := vm.ToValue(realpath)
  64. return reply
  65. }
  66. })
  67. //Check if a given virtual path is readonly
  68. vm.Set("pathCanWrite", func(call otto.FunctionCall) otto.Value {
  69. vpath, _ := call.Argument(0).ToString()
  70. if u.CanWrite(vpath) {
  71. return otto.TrueValue()
  72. } else {
  73. return otto.FalseValue()
  74. }
  75. })
  76. //Permission related
  77. vm.Set("getUserPermissionGroup", func(call otto.FunctionCall) otto.Value {
  78. groupinfo := u.GetUserPermissionGroup()
  79. jsonString, _ := json.Marshal(groupinfo)
  80. reply, _ := vm.ToValue(string(jsonString))
  81. return reply
  82. })
  83. vm.Set("userIsAdmin", func(call otto.FunctionCall) otto.Value {
  84. reply, _ := vm.ToValue(u.IsAdmin())
  85. return reply
  86. })
  87. //User Account Related
  88. /*
  89. userExists(username);
  90. */
  91. vm.Set("userExists", func(call otto.FunctionCall) otto.Value {
  92. if u.IsAdmin() {
  93. //Get username from function paramter
  94. username, err := call.Argument(0).ToString()
  95. if err != nil || username == "undefined" {
  96. g.raiseError(errors.New("username is undefined"))
  97. reply, _ := vm.ToValue(nil)
  98. return reply
  99. }
  100. //Check if user exists
  101. userExists := u.Parent().GetAuthAgent().UserExists(username)
  102. if userExists {
  103. return otto.TrueValue()
  104. } else {
  105. return otto.FalseValue()
  106. }
  107. } else {
  108. g.raiseError(errors.New("Permission Denied: userExists require admin permission"))
  109. return otto.FalseValue()
  110. }
  111. })
  112. /*
  113. createUser(username, password, defaultGroup);
  114. */
  115. vm.Set("createUser", func(call otto.FunctionCall) otto.Value {
  116. if u.IsAdmin() {
  117. //Ok. Create user base on given information
  118. username, err := call.Argument(0).ToString()
  119. if err != nil || username == "undefined" {
  120. g.raiseError(errors.New("username is undefined"))
  121. reply, _ := vm.ToValue(false)
  122. return reply
  123. }
  124. password, err := call.Argument(1).ToString()
  125. if err != nil || password == "undefined" {
  126. g.raiseError(errors.New("password is undefined"))
  127. reply, _ := vm.ToValue(false)
  128. return reply
  129. }
  130. defaultGroup, err := call.Argument(2).ToString()
  131. if err != nil || defaultGroup == "undefined" {
  132. g.raiseError(errors.New("defaultGroup is undefined"))
  133. reply, _ := vm.ToValue(false)
  134. return reply
  135. }
  136. //Check if username already used
  137. userExists := u.Parent().GetAuthAgent().UserExists(username)
  138. if userExists {
  139. g.raiseError(errors.New("Username already exists"))
  140. reply, _ := vm.ToValue(false)
  141. return reply
  142. }
  143. //Check if the given permission group exists
  144. groupExists := u.Parent().GetPermissionHandler().GroupExists(defaultGroup)
  145. if !groupExists {
  146. g.raiseError(errors.New(defaultGroup + " user-group not exists"))
  147. reply, _ := vm.ToValue(false)
  148. return reply
  149. }
  150. //Create the user
  151. err = u.Parent().GetAuthAgent().CreateUserAccount(username, password, []string{defaultGroup})
  152. if err != nil {
  153. g.raiseError(errors.New("User creation failed: " + err.Error()))
  154. reply, _ := vm.ToValue(false)
  155. return reply
  156. }
  157. return otto.TrueValue()
  158. } else {
  159. g.raiseError(errors.New("Permission Denied: createUser require admin permission"))
  160. return otto.FalseValue()
  161. }
  162. })
  163. vm.Set("editUser", func(call otto.FunctionCall) otto.Value {
  164. if u.IsAdmin() {
  165. } else {
  166. g.raiseError(errors.New("Permission Denied: editUser require admin permission"))
  167. return otto.FalseValue()
  168. }
  169. //libname, err := call.Argument(0).ToString()
  170. return otto.FalseValue()
  171. })
  172. /*
  173. removeUser(username)
  174. */
  175. vm.Set("removeUser", func(call otto.FunctionCall) otto.Value {
  176. if u.IsAdmin() {
  177. //Get username from function paramters
  178. username, err := call.Argument(0).ToString()
  179. if err != nil || username == "undefined" {
  180. g.raiseError(errors.New("username is undefined"))
  181. reply, _ := vm.ToValue(false)
  182. return reply
  183. }
  184. //Check if the user exists
  185. userExists := u.Parent().GetAuthAgent().UserExists(username)
  186. if !userExists {
  187. g.raiseError(errors.New(username + " not exists"))
  188. reply, _ := vm.ToValue(false)
  189. return reply
  190. }
  191. //User exists. Remove it from the system
  192. err = u.Parent().GetAuthAgent().UnregisterUser(username)
  193. if err != nil {
  194. g.raiseError(errors.New("User removal failed: " + err.Error()))
  195. reply, _ := vm.ToValue(false)
  196. return reply
  197. }
  198. return otto.TrueValue()
  199. } else {
  200. g.raiseError(errors.New("Permission Denied: removeUser require admin permission"))
  201. return otto.FalseValue()
  202. }
  203. })
  204. vm.Set("getUserInfoByName", func(call otto.FunctionCall) otto.Value {
  205. //libname, err := call.Argument(0).ToString()
  206. if u.IsAdmin() {
  207. } else {
  208. g.raiseError(errors.New("Permission Denied: getUserInfoByName require admin permission"))
  209. return otto.FalseValue()
  210. }
  211. return otto.TrueValue()
  212. })
  213. //Allow real time library includsion into the virtual machine
  214. vm.Set("requirelib", func(call otto.FunctionCall) otto.Value {
  215. libname, err := call.Argument(0).ToString()
  216. if err != nil {
  217. g.raiseError(err)
  218. reply, _ := vm.ToValue(nil)
  219. return reply
  220. }
  221. //Handle special case on high level libraries
  222. if libname == "websocket" && w != nil && r != nil {
  223. g.injectWebSocketFunctions(vm, u, w, r)
  224. return otto.TrueValue()
  225. } else {
  226. //Check if the library name exists. If yes, run the initiation script on the vm
  227. if entryPoint, ok := g.LoadedAGILibrary[libname]; ok {
  228. entryPoint(vm, u)
  229. return otto.TrueValue()
  230. } else {
  231. //Lib not exists
  232. log.Println("Lib not found: " + libname)
  233. return otto.FalseValue()
  234. }
  235. }
  236. //Unknown status
  237. return otto.FalseValue()
  238. })
  239. }