stash-box/pkg/api/authorization.go
InfiniteStash 5efb00c542 Fix roundup (#66)
* Prevent submission of fingerprints with 0s duration

* Escape email address in activation email link

* Sort edit comments by timestamp

* Use local timezone for edit/editnote timestamps

* Order user list ascending

* Disable submit buttons while edits are submitting.

* Fix performer/scene update triggers to concatenate and update correctly

* Add scene duration to scene page and search card

* Allow unsetting studio parent id

* Fix performer ordering by scene_count

* Add performer order by created_at

* Fix updating of column names with reserved names

* Allow users to cancel own edits

* Fix edit diff of partial performer birthdates

* Fix birthdate showing up in edit diffs when being removed

* Add career start ordering for performers

* Support searching by tag name with uuid

* Change edit diff checkboxes to icons for visibility

* Add markdown to edit comments

* Add scene filtering by fingerprint

* Switch markdown library to react-markdown

* Prevent generation of image uuids that start with ad

* Fix timestamp timezone

* Restore #60

Co-authored-by: peolic <66393006+peolic@users.noreply.github.com>

* Fix breasttype issues for male performers

* Fix breasttype issue for performers with null breasttype

* Add shortcut for changing performers in scenes

* Linting

* Coalesce breast type to Unknown rather than NA

* Fix breasttype diff logic

* Fix text truncation of performer name in scene form

Co-authored-by: peolic <66393006+peolic@users.noreply.github.com>
2021-05-09 13:38:38 +02:00

85 lines
1.6 KiB
Go

package api
import (
"context"
"errors"
"github.com/gofrs/uuid"
"github.com/stashapp/stash-box/pkg/models"
)
var ErrUnauthorized = errors.New("Not authorized")
func getCurrentUser(ctx context.Context) *models.User {
userCtxVal := ctx.Value(ContextUser)
if userCtxVal != nil {
currentUser := userCtxVal.(*models.User)
return currentUser
}
return nil
}
func validateRole(ctx context.Context, requiredRole models.RoleEnum) error {
var roles []models.RoleEnum
roleCtxVal := ctx.Value(ContextRoles)
if roleCtxVal != nil {
roles = roleCtxVal.([]models.RoleEnum)
}
valid := false
for _, role := range roles {
if role.Implies(requiredRole) {
valid = true
break
}
}
if !valid {
return ErrUnauthorized
}
return nil
}
func validateRead(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumRead)
}
func validateModify(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumModify)
}
func validateEdit(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumEdit)
}
func validateInvite(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumInvite)
}
func validateManageInvites(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumManageInvites)
}
func validateAdmin(ctx context.Context) error {
return validateRole(ctx, models.RoleEnumAdmin)
}
func validateOwner(ctx context.Context, userID uuid.UUID) error {
err := validateAdmin(ctx)
if err == nil {
return nil
}
user := getCurrentUser(ctx)
if user != nil && user.ID == userID {
return nil
}
return ErrUnauthorized
}