actions_resource.go 7.08 KB
Newer Older
1 2 3 4 5 6 7
package accountserver

import (
	"errors"
	"fmt"
)

ale's avatar
ale committed
8 9 10 11 12 13 14 15 16 17 18
// GetResourceRequest requests a specific resource.
type GetResourceRequest struct {
	AdminResourceRequestBase
}

// GetResourceResponse is the response type for GetResourceRequest.
type GetResourceResponse struct {
	Resource *Resource `json:"resource"`
	Owner    string    `json:"owner"`
}

ale's avatar
ale committed
19
// Serve the request.
ale's avatar
ale committed
20 21 22 23 24 25 26 27 28 29
func (r *GetResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
	resp := GetResourceResponse{
		Resource: rctx.Resource,
	}
	if rctx.User != nil {
		resp.Owner = rctx.User.Name
	}
	return &resp, nil
}

ale's avatar
ale committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// SearchResourceRequest searches for resources matching a pattern.
type SearchResourceRequest struct {
	AdminRequestBase

	Pattern string `json:"pattern"`
}

// Validate the request.
func (r *SearchResourceRequest) Validate(rctx *RequestContext) error {
	if r.Pattern == "" {
		return errors.New("empty pattern")
	}
	return nil
}

// SearchResourceResponse is the response type for SearchResourceRequest.
type SearchResourceResponse struct {
	Results []*RawResource `json:"results"`
}

// Serve the request.
func (r *SearchResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
	results, err := rctx.TX.SearchResource(rctx.Context, r.Pattern)
	if err != nil {
		return nil, err
	}
	return &SearchResourceResponse{Results: results}, nil
}

59 60 61 62 63 64 65 66
// setResourceStatus sets the status of a single resource (shared
// logic between enable / disable resource methods).
func setResourceStatus(rctx *RequestContext, status string) error {
	rsrc := rctx.Resource
	rsrc.Status = status
	if err := rctx.TX.UpdateResource(rctx.Context, rsrc); err != nil {
		return err
	}
ale's avatar
ale committed
67
	rctx.audit.Log(rctx, rsrc, fmt.Sprintf("status set to %s", status))
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	return nil
}

// DisableResourceRequest disables a resource belonging to the user.
type DisableResourceRequest struct {
	ResourceRequestBase
}

// Serve the request.
func (r *DisableResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
	return nil, setResourceStatus(rctx, ResourceStatusInactive)
}

// EnableResourceRequest enables a resource belonging to the user (admin-only).
type EnableResourceRequest struct {
	AdminResourceRequestBase
}

// Serve the request.
func (r *EnableResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
	return nil, setResourceStatus(rctx, ResourceStatusActive)
}

// ResetResourcePasswordRequest will reset the password associated
// with a resource (if the resource type supports it). It will
// generate a random password and return it to the caller.
type ResetResourcePasswordRequest struct {
	ResourceRequestBase
}

// ResetResourcePasswordResponse is the response type for
// ResetResourcePasswordRequest.
type ResetResourcePasswordResponse struct {
	Password string `json:"password"`
}

// Validate the request.
ale's avatar
ale committed
105 106
func (r *ResetResourcePasswordRequest) Validate(rctx *RequestContext) error {
	switch rctx.Resource.Type {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	case ResourceTypeDAV, ResourceTypeDatabase, ResourceTypeMailingList:
	case ResourceTypeEmail:
		return errors.New("can't reset email passwords with this API")
	default:
		return errors.New("can't reset password on this resource type")
	}
	return nil
}

// Serve the request.
func (r *ResetResourcePasswordRequest) Serve(rctx *RequestContext) (interface{}, error) {
	// TODO: this needs a resource-type switch, because in some
	// cases we may want to call out to other backends in order to
	// reset credentials for certain resources that have their own
	// secondary authentication databases (lists, mysql).
	password, err := doResetResourcePassword(rctx.Context, rctx.TX, rctx.Resource)
	if err != nil {
		return nil, err
	}
	return &ResetResourcePasswordResponse{
		Password: password,
	}, nil
}

// MoveResourceRequest is an administrative operation to move resources
// between shards. Resources that are part of a group are moved all at
// once regardless of which individual ResourceID is provided as long
// as it belongs to the group.
type MoveResourceRequest struct {
	AdminResourceRequestBase
	Shard string `json:"shard"`
}

// Validate the request.
func (r *MoveResourceRequest) Validate(rctx *RequestContext) error {
	// TODO: check shard
	return nil
}

// MoveResourceResponse is the response type for MoveResourceRequest.
type MoveResourceResponse struct {
	MovedIDs []string `json:"moved_ids"`
}

// Serve the request.
func (r *MoveResourceRequest) Serve(rctx *RequestContext) (interface{}, error) {
	resources := []*Resource{rctx.Resource}
ale's avatar
ale committed
154

155 156 157 158 159 160
	// If we have an associated user, collect all related
	// resources, as they should all be moved at once.
	if rctx.User != nil && rctx.Resource.Group != "" {
		resources = append(resources, rctx.User.GetResourcesByGroup(rctx.Resource.Group)...)
	}

ale's avatar
ale committed
161 162 163
	// We need to enforce consistency between email resources and
	// the user shard, so that temporary data can be colocated
	// with email storage.
ale's avatar
ale committed
164
	if rctx.Resource.Type == ResourceTypeEmail && rctx.User.Shard != r.Shard {
ale's avatar
ale committed
165 166 167 168 169 170
		rctx.User.Shard = r.Shard
		if err := rctx.TX.UpdateUser(rctx.Context, &rctx.User.User); err != nil {
			return nil, err
		}
	}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	var resp MoveResourceResponse
	for _, rsrc := range resources {
		rsrc.Shard = r.Shard
		if err := rctx.TX.UpdateResource(rctx.Context, rsrc); err != nil {
			return nil, err
		}
		resp.MovedIDs = append(resp.MovedIDs, rsrc.ID.String())
	}
	return &resp, nil
}

// AddEmailAliasRequest adds an alias (additional address) to an email resource.
type AddEmailAliasRequest struct {
	ResourceRequestBase
	Addr string `json:"addr"`
}

// Validate the request.
func (r *AddEmailAliasRequest) Validate(rctx *RequestContext) error {
ale's avatar
ale committed
190
	if rctx.Resource.Type != ResourceTypeEmail {
191 192 193 194 195 196 197 198
		return errors.New("this operation only works on email resources")
	}

	// Allow at most 5 aliases.
	if len(rctx.Resource.Email.Aliases) >= maxEmailAliases {
		return errors.New("too many aliases")
	}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	if err := rctx.fieldValidators.email(rctx.Context, r.Addr); err != nil {
		return newValidationError(nil, "addr", err.Error())
	}
	return nil
}

const maxEmailAliases = 5

// Serve the request.
func (r *AddEmailAliasRequest) Serve(rctx *RequestContext) (interface{}, error) {
	rctx.Resource.Email.Aliases = append(rctx.Resource.Email.Aliases, r.Addr)
	if err := rctx.TX.UpdateResource(rctx.Context, rctx.Resource); err != nil {
		return nil, err
	}

ale's avatar
ale committed
214
	rctx.audit.Log(rctx, rctx.Resource, fmt.Sprintf("added alias %s", r.Addr))
215 216 217 218 219 220 221 222 223
	return nil, nil
}

// DeleteEmailAliasRequest removes an alias from an email resource.
type DeleteEmailAliasRequest struct {
	ResourceRequestBase
	Addr string `json:"addr"`
}

224 225
// Validate the request.
func (r *DeleteEmailAliasRequest) Validate(rctx *RequestContext) error {
ale's avatar
ale committed
226
	if rctx.Resource.Type != ResourceTypeEmail {
227
		return errors.New("this operation only works on email resources")
228
	}
229 230
	return nil
}
231

232 233
// Serve the request.
func (r *DeleteEmailAliasRequest) Serve(rctx *RequestContext) (interface{}, error) {
234 235 236 237 238 239 240 241 242 243 244
	var aliases []string
	for _, a := range rctx.Resource.Email.Aliases {
		if a != r.Addr {
			aliases = append(aliases, a)
		}
	}
	rctx.Resource.Email.Aliases = aliases
	if err := rctx.TX.UpdateResource(rctx.Context, rctx.Resource); err != nil {
		return nil, err
	}

ale's avatar
ale committed
245
	rctx.audit.Log(rctx, rctx.Resource, fmt.Sprintf("removed alias %s", r.Addr))
246 247
	return nil, nil
}