Skip to content

Permission Service

Kronnox edited this page May 7, 2018 · 1 revision

The Permission Service makes it possible to assign permissions dynamically via a file. Even though the modder still sets a needed role for certain actions (like commands), it only determines a default role and not a mandatory one. The respective role belonging to the permission can be modified afterwards by the server owner or mod pack creator.

Permissions Services also offer another setting, so that nobody, no matter if admin or user, has access to the respective function. For example, if a command is not desired by the serverowner, it can be completely disabled and made inaccessible to everyone.

Defining a Permission Service

First, the field/property PermissionService of type IPermissionService has to be added to the mod.

[Inject]
public IPermissionService PermissionService { get; set; }

The [Inject]-attribute is needed in order for Asphalt to perform the Dependency-Injection later on.

Defining the permissions

In order to define the permission including the corresponding default values, the method GetPermissions, with the return type DefaultPermissions[], must be added to the same file.

[Inject]
public DefaultPermission[] GetDefaultPermissions()
{
    return new DefaultPermission[]
    {
        new DefaultPermission("advtp.help", PermissionGroup.User),
        new DefaultPermission("advtp.reload", PermissionGroup.Admin),
        ...
        new DefaultPermission("home.ignorelimit", PermissionGroup.Admin)
    };
}

The first argument of a DefaultPermission is the permission (permission-key) itself. The second argument is the default value that will be assigned to the respective permission.

Default value Usage
PermissionGroup.Admin only admins will be able to perform this action
PermissionGroup.User users and admins will be able to perform this action
PermissionGroup.Nobody nobody will be able to perform this action

Once this is done, the permission service is already set up. Asphalt takes care of everything else.

Using the Permission Service

The Permission Service currently provides two methods.

HasPermission

bool PermissionService.HasPermission(User user, string permissionKey)

Returns true if a user has the permission to perform the action.

Example usage:

[ChatCommand("helloworld", "Example Command")]
public static void HelloWorldCommand(User user)
{
    if (!PermissionService.HasPermission(user, "hello.world"))
        return;

    user.Player.SendTemporaryMessage("Hello World!");
}

If the Player does not have the permission "hello.world": no chat message at all

If the Player does have the permission "hello.world": "Hello World!"

CheckPermission

bool PermissionService.CheckPermission(User user, string permissionKey)

Returns true if a user has the permission to perform the action.

Example usage:

[ChatCommand("helloworld", "Example Command")]
public static void HelloWorldCommand(User user)
{
    if (!PermissionService.CheckPermission(user, "hello.world"))
        return;

    user.Player.SendTemporaryMessage("Hello World!");
}

If the Player does not have the permission "hello.world": "You don't have the permission to do this! Permission needed: hello.world"

If the Player does have the permission "hello.world": "Hello World!"

Home

Clone this wiki locally