2022-11-18 23:12:37 +01:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
2022-11-19 23:16:54 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2022-11-18 23:12:37 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PermKeyType string
|
|
|
|
|
|
|
|
const (
|
2022-11-19 23:16:54 +01:00
|
|
|
PermKeyTypeNone PermKeyType = "NONE" // (nothing)
|
|
|
|
PermKeyTypeUserSend PermKeyType = "USER_SEND" // send-messages
|
|
|
|
PermKeyTypeUserRead PermKeyType = "USER_READ" // send-messages, list-messages, read-user
|
|
|
|
PermKeyTypeUserAdmin PermKeyType = "USER_ADMIN" // send-messages, list-messages, read-user, delete-messages, update-user
|
2022-11-18 23:12:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type PermissionSet struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
UserID *models.UserID
|
2022-11-19 23:16:54 +01:00
|
|
|
KeyType PermKeyType
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmptyPermissions() PermissionSet {
|
|
|
|
return PermissionSet{
|
2022-11-19 23:16:54 +01:00
|
|
|
UserID: nil,
|
|
|
|
KeyType: PermKeyTypeNone,
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (ac *AppContext) CheckPermissionUserRead(userid models.UserID) *ginresp.HTTPResponse {
|
2022-11-18 23:12:37 +01:00
|
|
|
p := ac.permissions
|
2022-11-19 23:16:54 +01:00
|
|
|
if p.UserID != nil && *p.UserID == userid && p.KeyType == PermKeyTypeUserRead {
|
2022-11-18 23:12:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
if p.UserID != nil && *p.UserID == userid && p.KeyType == PermKeyTypeUserAdmin {
|
2022-11-18 23:12:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:34:18 +01:00
|
|
|
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
2022-11-18 23:28:37 +01:00
|
|
|
|
2022-11-20 00:19:41 +01:00
|
|
|
func (ac *AppContext) CheckPermissionRead() *ginresp.HTTPResponse {
|
|
|
|
p := ac.permissions
|
|
|
|
if p.UserID != nil && p.KeyType == PermKeyTypeUserRead {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if p.UserID != nil && p.KeyType == PermKeyTypeUserAdmin {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:34:18 +01:00
|
|
|
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
2022-11-20 00:19:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (ac *AppContext) CheckPermissionUserAdmin(userid models.UserID) *ginresp.HTTPResponse {
|
2022-11-18 23:28:37 +01:00
|
|
|
p := ac.permissions
|
2022-11-19 23:16:54 +01:00
|
|
|
if p.UserID != nil && *p.UserID == userid && p.KeyType == PermKeyTypeUserAdmin {
|
2022-11-18 23:28:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 20:34:18 +01:00
|
|
|
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppContext) CheckPermissionSend() *ginresp.HTTPResponse {
|
|
|
|
p := ac.permissions
|
|
|
|
if p.UserID != nil && p.KeyType == PermKeyTypeUserSend {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if p.UserID != nil && p.KeyType == PermKeyTypeUserAdmin {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
2022-11-18 23:28:37 +01:00
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
func (ac *AppContext) CheckPermissionAny() *ginresp.HTTPResponse {
|
|
|
|
p := ac.permissions
|
|
|
|
if p.KeyType == PermKeyTypeNone {
|
2022-11-20 20:34:18 +01:00
|
|
|
return langext.Ptr(ginresp.APIError(ac.ginContext, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil))
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppContext) CheckPermissionMessageReadDirect(msg models.Message) bool {
|
|
|
|
p := ac.permissions
|
|
|
|
if p.UserID != nil && msg.OwnerUserID == *p.UserID && p.KeyType == PermKeyTypeUserRead {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.UserID != nil && msg.OwnerUserID == *p.UserID && p.KeyType == PermKeyTypeUserAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (ac *AppContext) GetPermissionUserID() *models.UserID {
|
2022-11-19 23:16:54 +01:00
|
|
|
if ac.permissions.UserID == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return langext.Ptr(*ac.permissions.UserID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppContext) IsPermissionUserRead() bool {
|
|
|
|
p := ac.permissions
|
|
|
|
return p.KeyType == PermKeyTypeUserRead || p.KeyType == PermKeyTypeUserAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppContext) IsPermissionUserSend() bool {
|
|
|
|
p := ac.permissions
|
|
|
|
return p.KeyType == PermKeyTypeUserSend || p.KeyType == PermKeyTypeUserAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AppContext) IsPermissionUserAdmin() bool {
|
|
|
|
p := ac.permissions
|
|
|
|
return p.KeyType == PermKeyTypeUserAdmin
|
|
|
|
}
|