Commit fecc334c authored by ale's avatar ale

Replace enable/disable_resource with a new set_resource_status API

parent 8466ed04
......@@ -68,24 +68,25 @@ func setResourceStatus(rctx *RequestContext, status string) error {
return nil
}
// DisableResourceRequest disables a resource belonging to the user.
type DisableResourceRequest struct {
ResourceRequestBase
}
// SetResourceStatusRequest modifies the status of a resource
// belonging to the user (admin-only).
type SetResourceStatusRequest struct {
AdminResourceRequestBase
// Serve the request.
func (r *DisableResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
return nil, setResourceStatus(rctx, ResourceStatusInactive)
Status string `json:"status"`
}
// EnableResourceRequest enables a resource belonging to the user (admin-only).
type EnableResourceRequest struct {
AdminResourceRequestBase
// Validate the request.
func (r *SetResourceStatusRequest) Validate(rctx *RequestContext) error {
if !isValidStatusByResourceType(rctx.Resource.Type, r.Status) {
return errors.New("invalid or unknown status")
}
return nil
}
// Serve the request.
func (r *EnableResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
return nil, setResourceStatus(rctx, ResourceStatusActive)
func (r *SetResourceStatusRequest) Serve(rctx *RequestContext) (interface{}, error) {
return nil, setResourceStatus(rctx, r.Status)
}
// ResetResourcePasswordRequest will reset the password associated
......
......@@ -59,8 +59,7 @@ func New(service *as.AccountService, backend as.Backend) *APIServer {
s.Register("/api/user/delete_app_specific_password", &as.DeleteApplicationSpecificPasswordRequest{})
s.Register("/api/resource/get", &as.GetResourceRequest{})
s.Register("/api/resource/search", &as.SearchResourceRequest{})
s.Register("/api/resource/enable", &as.EnableResourceRequest{})
s.Register("/api/resource/disable", &as.DisableResourceRequest{})
s.Register("/api/resource/set_status", &as.SetResourceStatusRequest{})
s.Register("/api/resource/create", &as.CreateResourcesRequest{})
s.Register("/api/resource/move", &as.MoveResourceRequest{})
s.Register("/api/resource/reset_password", &as.ResetPasswordRequest{})
......
......@@ -392,6 +392,23 @@ const (
ResourceStatusArchived = "archived"
)
// Returns true if the given status is valid for the given resource type.
func isValidStatusByResourceType(rtype, rstatus string) bool {
switch rtype {
case ResourceTypeEmail, ResourceTypeMailingList:
switch rstatus {
case ResourceStatusActive, ResourceStatusInactive, ResourceStatusReadonly:
return true
}
case ResourceTypeWebsite, ResourceTypeDomain, ResourceTypeDAV, ResourceTypeDatabase:
switch rstatus {
case ResourceStatusActive, ResourceStatusInactive, ResourceStatusReadonly, ResourceStatusArchived:
return true
}
}
return false
}
// ResourceID is an opaque ID that uniquely identifies a resource in
// the backend database. These should normally not be visible to users.
type ResourceID string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment