Documentation
¶
Overview ¶
Package repository reads and writes the sharing table every vertical grants through.
Index ¶
- type Repository
- func (r *Repository) Delete(ctx context.Context, s *model.Sharing) error
- func (r *Repository) DeleteByPrincipal(ctx context.Context, principalType model.PrincipalType, principalID string) error
- func (r *Repository) DeleteByResources(ctx context.Context, resourceType model.ResourceType, resourceIDs []string) error
- func (r *Repository) FindByID(ctx context.Context, resourceType model.ResourceType, ...) (*model.Sharing, error)
- func (r *Repository) FindByPrincipal(ctx context.Context, resourceType model.ResourceType, resourceID string, ...) (*model.Sharing, error)
- func (r *Repository) FindByResource(ctx context.Context, resourceType model.ResourceType, resourceID string) ([]model.Sharing, error)
- func (r *Repository) FindByResources(ctx context.Context, resourceType model.ResourceType, resourceIDs []string) ([]model.Sharing, error)
- func (r *Repository) ResourceIDsSharedWith(ctx context.Context, resourceType model.ResourceType, userID string) ([]string, error)
- func (r *Repository) Save(ctx context.Context, s *model.Sharing) error
- func (r *Repository) WithTx(tx *gorm.DB) *Repository
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository reads and writes every share in the platform.
One repository for every resource kind, because the rows differ only in the resource type they name. Every method is scoped by that type: an id is unique within its own kind, not across them, so a lookup that omitted the type could return a product's share for a storage's id.
func NewRepository ¶
func NewRepository(db *gorm.DB) *Repository
NewRepository returns a repository backed by db.
func (*Repository) DeleteByPrincipal ¶
func (r *Repository) DeleteByPrincipal(ctx context.Context, principalType model.PrincipalType, principalID string) error
DeleteByPrincipal removes every share granted to one principal, across every resource kind.
It is what keeps a deleted group from leaving rows behind. Those rows would grant nothing — memberships are cascaded away with the group, so nobody resolves as an active member of it — but they would accumulate, and a listing of a resource's shares would name a group that no longer exists.
func (*Repository) DeleteByResources ¶
func (r *Repository) DeleteByResources(ctx context.Context, resourceType model.ResourceType, resourceIDs []string) error
DeleteByResources removes every share of any of resourceIDs.
No foreign key points at the resource, so nothing in the database removes these when the record they open up goes away: whatever deletes a resource calls this in the same transaction. For a virtual directory that means the whole subtree, since the directory rows below it are cascaded away by the database without their shares.
func (*Repository) FindByID ¶
func (r *Repository) FindByID(ctx context.Context, resourceType model.ResourceType, resourceID, sharingID string) (*model.Sharing, error)
FindByID returns one share scoped to its resource, or gorm.ErrRecordNotFound.
Scoping is deliberate: it stops a sharing id belonging to one resource from being reached through another resource's path.
func (*Repository) FindByPrincipal ¶
func (r *Repository) FindByPrincipal(ctx context.Context, resourceType model.ResourceType, resourceID string, principalType model.PrincipalType, principalID string) (*model.Sharing, error)
FindByPrincipal returns the share of one resource with one principal, or gorm.ErrRecordNotFound. It is what makes a duplicate share reportable as a conflict.
func (*Repository) FindByResource ¶
func (r *Repository) FindByResource(ctx context.Context, resourceType model.ResourceType, resourceID string) ([]model.Sharing, error)
FindByResource returns every share of one resource, of either principal kind.
Resolving access needs them together, which is the whole point of the single table: what used to be two queries against two tables is one.
func (*Repository) FindByResources ¶
func (r *Repository) FindByResources(ctx context.Context, resourceType model.ResourceType, resourceIDs []string) ([]model.Sharing, error)
FindByResources returns every share of any of resourceIDs.
A virtual directory inherits what its ancestors grant, so resolving one node asks about the whole chain at once rather than once per level.
func (*Repository) ResourceIDsSharedWith ¶
func (r *Repository) ResourceIDsSharedWith(ctx context.Context, resourceType model.ResourceType, userID string) ([]string, error)
ResourceIDsSharedWith returns the ids of one kind of resource reaching userID through a share: named directly, or through a group they are an active member of.
It replaces the pair of correlated subqueries each resource used to build against its own two tables. Callers filter out what the user already owns; a share is not how an owner reaches their own record.
func (*Repository) WithTx ¶
func (r *Repository) WithTx(tx *gorm.DB) *Repository
WithTx returns a repository bound to tx.