A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1// SiYuan - Refactor your thinking
2// Copyright (c) 2020-present, b3log.org
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package model
18
19import "github.com/gin-gonic/gin"
20
21type Role uint
22
23const (
24 RoleContextKey = "role"
25)
26
27const (
28 RoleAdministrator Role = iota // 管理员
29 RoleEditor // 编辑者
30 RoleReader // 读者
31 RoleVisitor // 匿名访问者
32)
33
34func IsValidRole(role Role, roles []Role) bool {
35 for _, role_ := range roles {
36 if role == role_ {
37 return true
38 }
39 }
40 return false
41}
42
43func IsReadOnlyRole(role Role) bool {
44 return IsValidRole(role, []Role{
45 RoleReader,
46 RoleVisitor,
47 })
48}
49
50func GetGinContextRole(c *gin.Context) Role {
51 if role, exists := c.Get(RoleContextKey); exists {
52 return role.(Role)
53 } else {
54 return RoleVisitor
55 }
56}
57
58func IsAdminRoleContext(c *gin.Context) bool {
59 return GetGinContextRole(c) == RoleAdministrator
60}