ripple-api/common/privileges.go

80 lines
3.2 KiB
Go
Raw Permalink Normal View History

2016-04-03 17:59:27 +00:00
package common
import "strings"
// These are the various privileges a token can have.
const (
PrivilegeRead = 1 << iota // used to be to fetch public data, such as user information etc. this is deprecated.
2016-04-03 17:59:27 +00:00
PrivilegeReadConfidential // (eventual) private messages, reports... of self
PrivilegeWrite // change user information, write into confidential stuff...
PrivilegeManageBadges // can change various users' badges.
PrivilegeBetaKeys // can add, remove, upgrade/downgrade, make public beta keys.
PrivilegeManageSettings // maintainance, set registrations, global alerts, bancho settings
PrivilegeViewUserAdvanced // can see user email, and perhaps warnings in the future, basically.
PrivilegeManageUser // can change user email, allowed status, userpage, rank, username...
PrivilegeManageRoles // translates as admin, as they can basically assign roles to anyone, even themselves
PrivilegeManageAPIKeys // admin permission to manage user permission, not only self permissions. Only ever do this if you completely trust the application, because this essentially means to put the entire ripple database in the hands of a (potentially evil?) application.
PrivilegeBlog // can do pretty much anything to the blog, and the documentation.
2016-04-07 17:32:48 +00:00
PrivilegeAPIMeta // can do /meta API calls. basically means they can restart the API server.
2016-04-27 18:03:06 +00:00
PrivilegeBeatmap // rank/unrank beatmaps. also BAT when implemented
2016-04-03 17:59:27 +00:00
)
// Privileges is a bitwise enum of the privileges of an user's API key.
2016-04-07 17:32:48 +00:00
type Privileges uint64
2016-04-03 17:59:27 +00:00
var privilegeString = [...]string{
"Read",
"ReadConfidential",
"Write",
"ManageBadges",
"BetaKeys",
"ManageSettings",
"ViewUserAdvanced",
"ManageUser",
"ManageRoles",
"ManageAPIKeys",
"Blog",
2016-04-07 17:32:48 +00:00
"APIMeta",
2016-04-27 18:03:06 +00:00
"Beatmap",
2016-04-03 17:59:27 +00:00
}
func (p Privileges) String() string {
var pvs []string
for i, v := range privilegeString {
if uint64(p)&uint64(1<<uint(i)) != 0 {
2016-04-03 17:59:27 +00:00
pvs = append(pvs, v)
}
}
return strings.Join(pvs, ", ")
}
2016-04-05 20:22:13 +00:00
var privilegeMustBe = [...]UserPrivileges{
1 << 30, // read is deprecated, and should be given out to no-one.
UserPrivilegeNormal,
UserPrivilegeNormal,
AdminPrivilegeAccessRAP | AdminPrivilegeManageBadges,
AdminPrivilegeAccessRAP | AdminPrivilegeManageBetaKey,
AdminPrivilegeAccessRAP | AdminPrivilegeManageSetting,
AdminPrivilegeAccessRAP,
AdminPrivilegeAccessRAP | AdminPrivilegeManageUsers | AdminPrivilegeBanUsers,
AdminPrivilegeAccessRAP | AdminPrivilegeManageUsers | AdminPrivilegeManagePrivilege,
AdminPrivilegeAccessRAP | AdminPrivilegeManageUsers | AdminPrivilegeManageServer,
AdminPrivilegeChatMod, // temporary?
AdminPrivilegeManageServer,
AdminPrivilegeAccessRAP | AdminPrivilegeManageBeatmap,
2016-04-05 20:22:13 +00:00
}
// CanOnly removes any privilege that the user has requested to have, but cannot have due to their rank.
func (p Privileges) CanOnly(userPrivs UserPrivileges) Privileges {
2016-04-05 20:22:13 +00:00
newPrivilege := 0
for i, v := range privilegeMustBe {
wants := p&1 == 1
can := userPrivs&v == v
2016-04-05 20:22:13 +00:00
if wants && can {
newPrivilege |= 1 << uint(i)
}
p >>= 1
}
return Privileges(newPrivilege)
}