aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/Role.java
blob: c63f341c6162aeea06e1c0c329d62da789fc2027 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.role;

import java.net.URI;
import java.util.Objects;

/**
 * A role is a combination of a {@link RoleDefinition} and a {@link Context}, which allows evaluation
 * of access control for a given action on a resource. Create using {@link Roles}.
 *
 * @author jonmv
 */
public abstract class Role {

    private final RoleDefinition roleDefinition;
    final Context context;

    Role(RoleDefinition roleDefinition, Context context) {
        this.roleDefinition = Objects.requireNonNull(roleDefinition);
        this.context = Objects.requireNonNull(context);
    }

    /** Returns the role definition of this bound role. */
    public RoleDefinition definition() { return roleDefinition; }

    /** Returns whether this role is allowed to perform the given action on the given resource. */
    public final boolean allows(Action action, URI uri, Enforcer enforcer) {
        return enforcer.allows(this, action, uri);
    }

    /** Returns whether the other role is a parent of this, and has a context included in this role's context. */
    public boolean implies(Role other) {
        return    (context.tenant().isEmpty() || context.tenant().equals(other.context.tenant()))
               && (context.application().isEmpty() || context.application().equals(other.context.application()))
               && roleDefinition.inherited().contains(other.roleDefinition);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Role role = (Role) o;
        return roleDefinition == role.roleDefinition &&
               Objects.equals(context, role.context);
    }

    @Override
    public int hashCode() {
        return Objects.hash(roleDefinition, context);
    }

}