func GroupJoinHandler(c echo.Context) error { session := session.NewSession(c) request := session.GetParam("request").(map[string]interface{}) password := "" if g, ok := request["group"]; ok { group := g.(map[string]interface{}) if group["password"] != nil { password = group["password"].(string) } } if session.User.Id != bson.ObjectId("") { id := bson.ObjectIdHex(session.GetParam("group_id").(string)) group, _ := session.GetApplication().FindGroupById(id) if group.CanJoin(session.User, password) { users := []model.User{session.User} group.AddUsers(users) group.Save() session.SetRecords("groups", []interface{}{group}) } } session.Write() return nil }
func FilesCreateHandler(c echo.Context) error { session := session.NewSession(c) request := session.GetParam("request").(map[string]interface{}) file_params := request["file"].(map[string]interface{}) var size int64 url := "" if file_params["size"] != nil { size, _ = file_params["size"].(json.Number).Int64() } if file_params["url"] != nil { url = file_params["url"].(string) } if url != "" || size != 0 { if !session.AuthFailed { record_id := bson.ObjectIdHex(session.GetParam("record_id").(string)) endpoint := session.GetEndpoint() record := endpoint.FindWriteRecordById(record_id, session.User) if record.Id != bson.ObjectId("") { file := model.File{} file.ApplicationId = session.GetApplication().Id file.RecordId = record.Id file.EndpointId = endpoint.Id file.Name = file_params["name"].(string) if size != 0 { file.Size = size } else { file.URL = url } file.Save() if file_params["size"] != nil { file.GenerateRequest() } record.AddFile(file) session.SetRecords("files", []model.File{file}) } } } else { session.SetError(1400) } session.Write() return nil }
func AdminListEndpointsHandler(c echo.Context) error { session := session.NewSession(c) if !session.AuthFailed && session.User.SuperUser == true { app := session.GetApplication() session.SetRecords("endpoints", app.AllEndpoints()) } session.Write() return nil }
func UsersAuthenticateHandler(c echo.Context) error { session := session.NewSession(c) request_params := session.GetParam("request").(map[string]interface{}) user_params := request_params["user"].(map[string]interface{}) valid := false user := model.User{} if user_params["email"] != nil && user_params["password"] != nil { email := user_params["email"].(string) password := user_params["password"].(string) user = session.GetApplication().FindUserByEmail(email) valid = user.VerifyPassword(password) } else if user_params["facebook_id"] != nil && user_params["facebook_token"] != nil { facebook_id := user_params["facebook_id"].(string) facebook_token := user_params["facebook_token"].(string) user = session.GetApplication().FindUserByFacebookId(facebook_id) valid = user.VerifyFacebookToken(facebook_token) } else if user_params["transfer_id"] != nil && user_params["transfer_token"] != nil { user_id := user_params["transfer_id"].(string) transfer_token := user_params["transfer_token"].(string) user = session.GetApplication().FindUserByTransferId(user_id) valid = user.VerifyTransferToken(transfer_token) } if !valid || user.Id == bson.ObjectId("") { session.SetError(1101) } else { user.NewSession() session.SetRecords("users", []interface{}{user}) } session.Write() return nil }
func RecordsBulkQueryHandler(c echo.Context) error { s := session.NewSession(c) if !s.AuthFailed { request := s.GetParam("request").(map[string]interface{}) requests := request["requests"].([]interface{}) c := make(chan bool, len(requests)) for _, request := range requests { go func(request interface{}, session *session.Session) { endpoint := session.GetApplication().FindEndpointByName(request.(map[string]interface{})["endpoint"].(string)) guid := request.(map[string]interface{})["guid"].(string) t := time.Now() if plugin.CheckPlugin("query", endpoint) { s := session.CopySessionForBulkRequest(guid) plugin.RunPlugin("query", endpoint, s) session.CopyResponseFromSessionForBulkRequest(s, guid, endpoint.Name) } else { r := request.(map[string]interface{})["request"].(map[string]interface{}) records, count := endpoint.FindReadRecordsWhere(r["where"].(map[string]interface{}), session.User) session.SetRecordsAndCalculateCapabilitiesForUserForGuid(endpoint.Name, records, session.User, guid) session.SetRecordCountForGuid(count, guid) } logger.Log("Bulk Loaded:", "/"+endpoint.ApplicationId.Hex()+"/"+endpoint.Name+"/query", time.Now().Sub(t).String(), session.User.Id.Hex()) c <- true }(request, &s) } i := 0 for { <-c i++ if i >= len(requests) { break } } } s.Write() return nil }
func GroupDestroyHandler(c echo.Context) error { session := session.NewSession(c) if !session.AuthFailed { id := bson.ObjectIdHex(session.GetParam("group_id").(string)) group, _ := session.GetApplication().FindGroupById(id) if group.CanModify(session.User) { group.Delete() } else { session.SetError(1002) } } else { session.SetError(1001) } session.Write() return nil }
func UsersUpdateHandler(c echo.Context) error { session := session.NewSession(c) request_params := session.GetParam("request").(map[string]interface{}) user_params := request_params["user"].(map[string]interface{}) user_id := session.GetParam("user_id").(string) user := session.GetApplication().FindUserById(bson.ObjectIdHex(user_id)) can_edit := session.User.SuperUser || session.User.Id == user.Id password := user_params["current_password"] valid := session.User.SuperUser || (password != nil && user.VerifyPassword(password.(string))) //fmt.Println(can_edit, user_id) if user.Id == bson.ObjectId("") || !can_edit { session.SetError(1101) } else if !valid { session.SetError(1106) } else { if user_params["name"] != nil { user.Name = user_params["name"].(string) } if user_params["email"] != nil { user.Email = user_params["email"].(string) } if user_params["password"] != nil { user.SetPassword(user_params["password"].(string)) } user.Save() session.SetRecords("users", []interface{}{user}) } session.Write() return nil }
func UsersGetHandler(c echo.Context) error { session := session.NewSession(c) user_id := session.GetParam("user_id").(string) user := session.GetApplication().FindUserById(bson.ObjectIdHex(user_id)) if !session.AuthFailed { // Only Send Private Profile if the user matches if session.User.Id == user.Id { user.SendPrivateProfile() } session.SetRecords("users", []interface{}{user}) } session.Write() return nil }
func GroupLeaveHandler(c echo.Context) error { session := session.NewSession(c) if session.User.Id != bson.ObjectId("") { id := bson.ObjectIdHex(session.GetParam("group_id").(string)) group, _ := session.GetApplication().FindGroupById(id) users := []model.User{session.User} group.RemoveUsers(users) group.Save() session.SetRecords("groups", []interface{}{group}) } session.Write() return nil }
func GroupUpdateHandler(c echo.Context) error { session := session.NewSession(c) if !session.AuthFailed { group_params := session.GetParam("request").(map[string]interface{}) id := bson.ObjectIdHex(session.GetParam("group_id").(string)) group, _ := session.GetApplication().FindGroupById(id) if group.CanModify(session.User) { group.Update(group_params) group.Save() } session.SetRecords("groups", []interface{}{group}) } session.Write() return nil }