2018-05-14 16:26:38 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Authorization is a authorization. 🎉
|
|
|
|
type Authorization struct {
|
2018-10-12 01:06:43 +00:00
|
|
|
ID ID `json:"id,omitempty"`
|
2018-05-14 16:26:38 +00:00
|
|
|
Token string `json:"token"`
|
2018-08-28 17:58:38 +00:00
|
|
|
Status Status `json:"status"`
|
2018-05-16 18:59:35 +00:00
|
|
|
User string `json:"user,omitempty"`
|
2018-07-30 14:29:52 +00:00
|
|
|
UserID ID `json:"userID,omitempty"`
|
2018-09-10 20:56:11 +00:00
|
|
|
Permissions []Permission `json:"permissions,omitempty"`
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:02:34 +00:00
|
|
|
// Allowed returns true if the authorization is active and request permission
|
2018-08-28 17:58:38 +00:00
|
|
|
// exists in the authorization's list of permissions.
|
2018-09-28 17:02:34 +00:00
|
|
|
func (a *Authorization) Allowed(p Permission) bool {
|
|
|
|
if !a.IsActive() {
|
2018-08-28 17:58:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:02:34 +00:00
|
|
|
return allowed(p, a.Permissions)
|
2018-08-28 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:41:46 +00:00
|
|
|
// IsActive is a stub for idpe.
|
|
|
|
func IsActive(a *Authorization) bool {
|
|
|
|
return a.IsActive()
|
|
|
|
}
|
|
|
|
|
2018-08-28 17:58:38 +00:00
|
|
|
// IsActive returns true if the authorization active.
|
2018-09-28 17:02:34 +00:00
|
|
|
func (a *Authorization) IsActive() bool {
|
|
|
|
return a.Status == Active
|
2018-08-28 17:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 18:33:35 +00:00
|
|
|
// Kind returns session and is used for auditing.
|
|
|
|
func (a *Authorization) Kind() string { return "authorization" }
|
|
|
|
|
2018-10-01 20:04:43 +00:00
|
|
|
// Identifier returns the authorizations ID and is used for auditing.
|
2018-09-28 18:33:35 +00:00
|
|
|
func (a *Authorization) Identifier() ID { return a.ID }
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// AuthorizationService represents a service for managing authorization data.
|
|
|
|
type AuthorizationService interface {
|
2018-05-16 18:59:35 +00:00
|
|
|
// Returns a single authorization by ID.
|
|
|
|
FindAuthorizationByID(ctx context.Context, id ID) (*Authorization, error)
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// Returns a single authorization by Token.
|
|
|
|
FindAuthorizationByToken(ctx context.Context, t string) (*Authorization, error)
|
|
|
|
|
|
|
|
// Returns a list of authorizations that match filter and the total count of matching authorizations.
|
|
|
|
// Additional options provide pagination & sorting.
|
|
|
|
FindAuthorizations(ctx context.Context, filter AuthorizationFilter, opt ...FindOptions) ([]*Authorization, int, error)
|
|
|
|
|
2018-05-16 18:59:35 +00:00
|
|
|
// Creates a new authorization and sets a.Token and a.UserID with the new identifier.
|
2018-05-14 16:26:38 +00:00
|
|
|
CreateAuthorization(ctx context.Context, a *Authorization) error
|
|
|
|
|
2018-08-28 17:58:38 +00:00
|
|
|
// SetAuthorizationStatus updates the status of the authorization. Useful
|
|
|
|
// for setting an authorization to inactive or active.
|
|
|
|
SetAuthorizationStatus(ctx context.Context, id ID, status Status) error
|
2018-08-27 19:18:11 +00:00
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// Removes a authorization by token.
|
2018-07-25 15:26:45 +00:00
|
|
|
DeleteAuthorization(ctx context.Context, id ID) error
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizationFilter represents a set of filter that restrict the returned results.
|
|
|
|
type AuthorizationFilter struct {
|
2018-05-16 18:59:35 +00:00
|
|
|
Token *string
|
|
|
|
ID *ID
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
UserID *ID
|
2018-05-16 18:59:35 +00:00
|
|
|
User *string
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|