SimpleCloudNotifier/scnserver/logic/permissions.go

98 lines
3.0 KiB
Go
Raw Normal View History

2022-11-18 23:12:37 +01:00
package logic
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
2022-12-20 13:55:09 +01:00
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/models"
2022-11-18 23:12:37 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
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
2023-01-13 17:17:17 +01:00
if p.UserID != nil && *p.UserID == userid && p.KeyType == models.PermKeyTypeUserRead {
2022-11-18 23:12:37 +01:00
return nil
}
2023-01-13 17:17:17 +01:00
if p.UserID != nil && *p.UserID == userid && p.KeyType == models.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
2023-01-13 17:17:17 +01:00
if p.UserID != nil && p.KeyType == models.PermKeyTypeUserRead {
2022-11-20 00:19:41 +01:00
return nil
}
2023-01-13 17:17:17 +01:00
if p.UserID != nil && p.KeyType == models.PermKeyTypeUserAdmin {
2022-11-20 00:19:41 +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-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
2023-01-13 17:17:17 +01:00
if p.UserID != nil && *p.UserID == userid && p.KeyType == models.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
2023-01-13 17:17:17 +01:00
if p.UserID != nil && p.KeyType == models.PermKeyTypeUserSend {
2022-11-20 20:34:18 +01:00
return nil
}
2023-01-13 17:17:17 +01:00
if p.UserID != nil && p.KeyType == models.PermKeyTypeUserAdmin {
2022-11-20 20:34:18 +01:00
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
}
func (ac *AppContext) CheckPermissionAny() *ginresp.HTTPResponse {
p := ac.permissions
2023-01-13 17:17:17 +01:00
if p.KeyType == models.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))
}
return nil
}
func (ac *AppContext) CheckPermissionMessageReadDirect(msg models.Message) bool {
p := ac.permissions
2023-01-13 17:17:17 +01:00
if p.UserID != nil && msg.OwnerUserID == *p.UserID && p.KeyType == models.PermKeyTypeUserRead {
return true
}
2023-01-13 17:17:17 +01:00
if p.UserID != nil && msg.OwnerUserID == *p.UserID && p.KeyType == models.PermKeyTypeUserAdmin {
return true
}
return false
}
2022-11-20 22:18:24 +01:00
func (ac *AppContext) GetPermissionUserID() *models.UserID {
if ac.permissions.UserID == nil {
return nil
} else {
return langext.Ptr(*ac.permissions.UserID)
}
}
func (ac *AppContext) IsPermissionUserRead() bool {
p := ac.permissions
2023-01-13 17:17:17 +01:00
return p.KeyType == models.PermKeyTypeUserRead || p.KeyType == models.PermKeyTypeUserAdmin
}
func (ac *AppContext) IsPermissionUserSend() bool {
p := ac.permissions
2023-01-13 17:17:17 +01:00
return p.KeyType == models.PermKeyTypeUserSend || p.KeyType == models.PermKeyTypeUserAdmin
}
func (ac *AppContext) IsPermissionUserAdmin() bool {
p := ac.permissions
2023-01-13 17:17:17 +01:00
return p.KeyType == models.PermKeyTypeUserAdmin
}