func (hooks UserResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response { meta := r.GetMeta() adaptor := meta.String("adaptor") if adaptor == "" { return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true) } rawData, ok := meta.Get("authData") if !ok { return kit.NewErrorResponse("auth_data_missing", "Expected 'authData' in metadata.", true) } data, ok := rawData.(map[string]interface{}) if !ok { return kit.NewErrorResponse("invalid_auth_data", "Invalid auth data: expected dictionary", true) } user := obj.(kit.User) service := res.Registry().UserService() // If a profile model was registered, and profile data is in meta, // create the profile model. if profiles := service.ProfileResource(); profiles != nil { profile := profiles.CreateModel().(kit.UserProfile) if rawData, ok := meta.Get("profile"); ok { if data, ok := rawData.(map[string]interface{}); ok { // Profile data present in meta. // Update profile with data. if err := res.ModelInfo().UpdateModelFromData(profile, data); err != nil { return kit.NewErrorResponse("invalid_profile_data", "Invalid profile data.", err, true) } } } user.SetProfile(profile) } if err := service.CreateUser(user, adaptor, data); err != nil { return kit.NewErrorResponse(err) } return &kit.AppResponse{ Data: user, } }
func (PageResource) Methods(res kit.Resource) []kit.Method { publish := &methods.Method{ Name: "cms.page.publish", Blocking: true, Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response { user := r.GetUser() if user == nil || !user.HasRole("admin") { return kit.NewErrorResponse("permission_denied") } id := utils.GetMapStringKey(r.GetData(), "id") if id == "" { return kit.NewErrorResponse("no_id_in_data", "Expected 'id' key in data.") } rawPage, err := res.Backend().FindOne("pages", id) if err != nil { return kit.NewErrorResponse("db_error", err) } else if rawPage == nil { return kit.NewErrorResponse("not_found", "The specified page id does not exist.") } err = res.ModelInfo().UpdateModelFromData(rawPage, map[string]interface{}{ "published": true, "published_at": time.Now(), }) if err != nil { return kit.NewErrorResponse("db_error", err) } return &kit.AppResponse{ Data: map[string]interface{}{"success": true}, } }, } return []kit.Method{publish} }