Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.server.schema;
16 :
17 : import com.google.gerrit.entities.AccessSection;
18 : import com.google.gerrit.entities.GroupReference;
19 : import com.google.gerrit.entities.LabelType;
20 : import com.google.gerrit.entities.Permission;
21 : import com.google.gerrit.entities.PermissionRule;
22 : import com.google.gerrit.server.project.ProjectConfig;
23 :
24 : /**
25 : * Contains functions to modify permissions. For all these functions, any of the groups may be null
26 : * in which case it is ignored.
27 : */
28 0 : public class AclUtil {
29 : public static void grant(
30 : ProjectConfig config,
31 : AccessSection.Builder section,
32 : String permission,
33 : GroupReference... groupList) {
34 151 : grant(config, section, permission, false, groupList);
35 151 : }
36 :
37 : public static void grant(
38 : ProjectConfig config,
39 : AccessSection.Builder section,
40 : String permission,
41 : boolean force,
42 : GroupReference... groupList) {
43 151 : grant(config, section, permission, force, null, groupList);
44 151 : }
45 :
46 : public static void grant(
47 : ProjectConfig config,
48 : AccessSection.Builder section,
49 : String permission,
50 : boolean force,
51 : Boolean exclusive,
52 : GroupReference... groupList) {
53 151 : Permission.Builder p = section.upsertPermission(permission);
54 151 : if (exclusive != null) {
55 151 : p.setExclusiveGroup(exclusive);
56 : }
57 151 : for (GroupReference group : groupList) {
58 151 : if (group != null) {
59 151 : p.add(rule(config, group).setForce(force));
60 : }
61 : }
62 151 : }
63 :
64 : public static void block(
65 : ProjectConfig config,
66 : AccessSection.Builder section,
67 : String permission,
68 : GroupReference... groupList) {
69 0 : Permission.Builder p = section.upsertPermission(permission);
70 0 : for (GroupReference group : groupList) {
71 0 : if (group != null) {
72 0 : p.add(rule(config, group).setBlock());
73 : }
74 : }
75 0 : }
76 :
77 : public static void grant(
78 : ProjectConfig config,
79 : AccessSection.Builder section,
80 : LabelType type,
81 : int min,
82 : int max,
83 : GroupReference... groupList) {
84 151 : grant(config, section, type, min, max, false, groupList);
85 151 : }
86 :
87 : public static void grant(
88 : ProjectConfig config,
89 : AccessSection.Builder section,
90 : LabelType type,
91 : int min,
92 : int max,
93 : boolean exclusive,
94 : GroupReference... groupList) {
95 151 : String name = Permission.LABEL + type.getName();
96 151 : Permission.Builder p = section.upsertPermission(name);
97 151 : p.setExclusiveGroup(exclusive);
98 151 : for (GroupReference group : groupList) {
99 151 : if (group != null) {
100 151 : p.add(rule(config, group).setRange(min, max));
101 : }
102 : }
103 151 : }
104 :
105 : public static PermissionRule.Builder rule(ProjectConfig config, GroupReference group) {
106 151 : return PermissionRule.builder(config.resolve(group));
107 : }
108 :
109 : public static void remove(
110 : ProjectConfig config,
111 : AccessSection.Builder section,
112 : String permission,
113 : GroupReference... groupList) {
114 1 : Permission.Builder p = section.upsertPermission(permission);
115 1 : for (GroupReference group : groupList) {
116 1 : if (group != null) {
117 1 : p.remove(rule(config, group).build());
118 : }
119 : }
120 1 : }
121 : }
|