Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
bb07d9ea
Commit
bb07d9ea
authored
Jun 16, 2022
by
4Benj_
Committed by
GitHub
Jun 16, 2022
Browse files
Custom Permission Handler (#1282)
Co-authored-by:
Melledy
<
52122272+Melledy@users.noreply.github.com
>
parent
6d59159b
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/Grasscutter.java
View file @
bb07d9ea
...
@@ -6,6 +6,8 @@ import java.util.Calendar;
...
@@ -6,6 +6,8 @@ import java.util.Calendar;
import
emu.grasscutter.auth.AuthenticationSystem
;
import
emu.grasscutter.auth.AuthenticationSystem
;
import
emu.grasscutter.auth.DefaultAuthentication
;
import
emu.grasscutter.auth.DefaultAuthentication
;
import
emu.grasscutter.command.CommandMap
;
import
emu.grasscutter.command.CommandMap
;
import
emu.grasscutter.command.DefaultPermissionHandler
;
import
emu.grasscutter.command.PermissionHandler
;
import
emu.grasscutter.game.managers.energy.EnergyManager
;
import
emu.grasscutter.game.managers.energy.EnergyManager
;
import
emu.grasscutter.game.managers.stamina.StaminaManager
;
import
emu.grasscutter.game.managers.stamina.StaminaManager
;
import
emu.grasscutter.plugin.PluginManager
;
import
emu.grasscutter.plugin.PluginManager
;
...
@@ -58,6 +60,7 @@ public final class Grasscutter {
...
@@ -58,6 +60,7 @@ public final class Grasscutter {
private
static
GameServer
gameServer
;
private
static
GameServer
gameServer
;
private
static
PluginManager
pluginManager
;
private
static
PluginManager
pluginManager
;
private
static
AuthenticationSystem
authenticationSystem
;
private
static
AuthenticationSystem
authenticationSystem
;
private
static
PermissionHandler
permissionHandler
;
public
static
final
Reflections
reflector
=
new
Reflections
(
"emu.grasscutter"
);
public
static
final
Reflections
reflector
=
new
Reflections
(
"emu.grasscutter"
);
public
static
ConfigContainer
config
;
public
static
ConfigContainer
config
;
...
@@ -114,8 +117,9 @@ public final class Grasscutter {
...
@@ -114,8 +117,9 @@ public final class Grasscutter {
// Initialize database.
// Initialize database.
DatabaseManager
.
initialize
();
DatabaseManager
.
initialize
();
// Initialize the default
authentication
system.
// Initialize the default system
s
.
authenticationSystem
=
new
DefaultAuthentication
();
authenticationSystem
=
new
DefaultAuthentication
();
permissionHandler
=
new
DefaultPermissionHandler
();
// Create server instances.
// Create server instances.
httpServer
=
new
HttpServer
();
httpServer
=
new
HttpServer
();
...
@@ -287,6 +291,10 @@ public final class Grasscutter {
...
@@ -287,6 +291,10 @@ public final class Grasscutter {
return
authenticationSystem
;
return
authenticationSystem
;
}
}
public
static
PermissionHandler
getPermissionHandler
()
{
return
permissionHandler
;
}
public
static
int
getCurrentDayOfWeek
()
{
public
static
int
getCurrentDayOfWeek
()
{
return
day
;
return
day
;
}
}
...
@@ -346,6 +354,14 @@ public final class Grasscutter {
...
@@ -346,6 +354,14 @@ public final class Grasscutter {
Grasscutter
.
authenticationSystem
=
authenticationSystem
;
Grasscutter
.
authenticationSystem
=
authenticationSystem
;
}
}
/**
* Sets the permission handler for the server.
* @param permissionHandler The permission handler to use.
*/
public
static
void
setPermissionHandler
(
PermissionHandler
permissionHandler
)
{
Grasscutter
.
permissionHandler
=
permissionHandler
;
}
/*
/*
* Enums for the configuration.
* Enums for the configuration.
*/
*/
...
...
src/main/java/emu/grasscutter/command/CommandMap.java
View file @
bb07d9ea
...
@@ -14,6 +14,7 @@ public final class CommandMap {
...
@@ -14,6 +14,7 @@ public final class CommandMap {
private
final
Map
<
String
,
Command
>
annotations
=
new
HashMap
<>();
private
final
Map
<
String
,
Command
>
annotations
=
new
HashMap
<>();
private
final
Map
<
String
,
Integer
>
targetPlayerIds
=
new
HashMap
<>();
private
final
Map
<
String
,
Integer
>
targetPlayerIds
=
new
HashMap
<>();
private
static
final
String
consoleId
=
"console"
;
private
static
final
String
consoleId
=
"console"
;
public
CommandMap
()
{
public
CommandMap
()
{
this
(
false
);
this
(
false
);
}
}
...
@@ -202,21 +203,9 @@ public final class CommandMap {
...
@@ -202,21 +203,9 @@ public final class CommandMap {
}
}
}
}
// Check for permission.
// Check for permissions.
if
(
player
!=
null
)
{
if
(!
Grasscutter
.
getPermissionHandler
().
checkPermission
(
player
,
targetPlayer
,
this
.
annotations
.
get
(
label
).
permission
(),
this
.
annotations
.
get
(
label
).
permissionTargeted
()))
{
String
permissionNode
=
this
.
annotations
.
get
(
label
).
permission
();
return
;
String
permissionNodeTargeted
=
this
.
annotations
.
get
(
label
).
permissionTargeted
();
Account
account
=
player
.
getAccount
();
if
(
player
!=
targetPlayer
)
{
// Additional permission required for targeting another player
if
(!
permissionNodeTargeted
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNodeTargeted
))
{
CommandHandler
.
sendTranslatedMessage
(
player
,
"commands.generic.permission_error"
);
return
;
}
}
if
(!
permissionNode
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNode
))
{
CommandHandler
.
sendTranslatedMessage
(
player
,
"commands.generic.permission_error"
);
return
;
}
}
}
// Check if command has unfulfilled constraints on targetPlayer
// Check if command has unfulfilled constraints on targetPlayer
...
...
src/main/java/emu/grasscutter/command/DefaultPermissionHandler.java
0 → 100644
View file @
bb07d9ea
package
emu.grasscutter.command
;
import
emu.grasscutter.game.Account
;
import
emu.grasscutter.game.player.Player
;
public
class
DefaultPermissionHandler
implements
PermissionHandler
{
@Override
public
boolean
EnablePermissionCommand
()
{
return
true
;
}
@Override
public
boolean
checkPermission
(
Player
player
,
Player
targetPlayer
,
String
permissionNode
,
String
permissionNodeTargeted
)
{
if
(
player
==
null
)
{
return
true
;
}
Account
account
=
player
.
getAccount
();
if
(
player
!=
targetPlayer
)
{
// Additional permission required for targeting another player
if
(!
permissionNodeTargeted
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNodeTargeted
))
{
CommandHandler
.
sendTranslatedMessage
(
player
,
"commands.generic.permission_error"
);
return
false
;
}
}
if
(!
permissionNode
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNode
))
{
CommandHandler
.
sendTranslatedMessage
(
player
,
"commands.generic.permission_error"
);
return
false
;
}
return
true
;
}
}
src/main/java/emu/grasscutter/command/PermissionHandler.java
0 → 100644
View file @
bb07d9ea
package
emu.grasscutter.command
;
import
emu.grasscutter.game.player.Player
;
public
interface
PermissionHandler
{
public
boolean
EnablePermissionCommand
();
public
boolean
checkPermission
(
Player
player
,
Player
targetPlayer
,
String
permissionNode
,
String
permissionNodeTargeted
);
}
src/main/java/emu/grasscutter/command/commands/PermissionCommand.java
View file @
bb07d9ea
...
@@ -20,6 +20,11 @@ public final class PermissionCommand implements CommandHandler {
...
@@ -20,6 +20,11 @@ public final class PermissionCommand implements CommandHandler {
return
;
return
;
}
}
if
(!
Grasscutter
.
getPermissionHandler
().
EnablePermissionCommand
())
{
CommandHandler
.
sendTranslatedMessage
(
sender
,
"commands.generic.permission_error"
);
return
;
}
String
action
=
args
.
get
(
0
);
String
action
=
args
.
get
(
0
);
String
permission
=
args
.
get
(
1
);
String
permission
=
args
.
get
(
1
);
...
...
src/main/java/emu/grasscutter/plugin/api/ServerHook.java
View file @
bb07d9ea
...
@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
...
@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import
emu.grasscutter.auth.AuthenticationSystem
;
import
emu.grasscutter.auth.AuthenticationSystem
;
import
emu.grasscutter.command.Command
;
import
emu.grasscutter.command.Command
;
import
emu.grasscutter.command.CommandHandler
;
import
emu.grasscutter.command.CommandHandler
;
import
emu.grasscutter.command.PermissionHandler
;
import
emu.grasscutter.game.player.Player
;
import
emu.grasscutter.game.player.Player
;
import
emu.grasscutter.server.game.GameServer
;
import
emu.grasscutter.server.game.GameServer
;
import
emu.grasscutter.server.http.HttpServer
;
import
emu.grasscutter.server.http.HttpServer
;
...
@@ -97,4 +98,12 @@ public final class ServerHook {
...
@@ -97,4 +98,12 @@ public final class ServerHook {
public
void
setAuthSystem
(
AuthenticationSystem
authSystem
)
{
public
void
setAuthSystem
(
AuthenticationSystem
authSystem
)
{
Grasscutter
.
setAuthenticationSystem
(
authSystem
);
Grasscutter
.
setAuthenticationSystem
(
authSystem
);
}
}
/**
* Sets the server's permission handler.
* @param permHandler An instance of the permission handler.
*/
public
void
setPermissionHandler
(
PermissionHandler
permHandler
)
{
Grasscutter
.
setPermissionHandler
(
permHandler
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment