summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/api/groups/GroupsImpl.java
blob: e1e72ba7850e7cb975d0dcc15a0a4a78138e5f24 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.api.groups;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gerrit.server.api.ApiUtil.asRestApiException;

import com.google.gerrit.extensions.api.groups.GroupApi;
import com.google.gerrit.extensions.api.groups.GroupInput;
import com.google.gerrit.extensions.api.groups.Groups;
import com.google.gerrit.extensions.client.ListGroupsOption;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.group.CreateGroup;
import com.google.gerrit.server.group.GroupsCollection;
import com.google.gerrit.server.group.ListGroups;
import com.google.gerrit.server.group.QueryGroups;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.project.ProjectsCollection;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.util.List;
import java.util.SortedMap;

@Singleton
class GroupsImpl implements Groups {
  private final AccountsCollection accounts;
  private final GroupsCollection groups;
  private final ProjectsCollection projects;
  private final Provider<ListGroups> listGroups;
  private final Provider<QueryGroups> queryGroups;
  private final Provider<CurrentUser> user;
  private final PermissionBackend permissionBackend;
  private final CreateGroup.Factory createGroup;
  private final GroupApiImpl.Factory api;

  @Inject
  GroupsImpl(
      AccountsCollection accounts,
      GroupsCollection groups,
      ProjectsCollection projects,
      Provider<ListGroups> listGroups,
      Provider<QueryGroups> queryGroups,
      Provider<CurrentUser> user,
      PermissionBackend permissionBackend,
      CreateGroup.Factory createGroup,
      GroupApiImpl.Factory api) {
    this.accounts = accounts;
    this.groups = groups;
    this.projects = projects;
    this.listGroups = listGroups;
    this.queryGroups = queryGroups;
    this.user = user;
    this.permissionBackend = permissionBackend;
    this.createGroup = createGroup;
    this.api = api;
  }

  @Override
  public GroupApi id(String id) throws RestApiException {
    return api.create(groups.parse(TopLevelResource.INSTANCE, IdString.fromDecoded(id)));
  }

  @Override
  public GroupApi create(String name) throws RestApiException {
    GroupInput in = new GroupInput();
    in.name = name;
    return create(in);
  }

  @Override
  public GroupApi create(GroupInput in) throws RestApiException {
    if (checkNotNull(in, "GroupInput").name == null) {
      throw new BadRequestException("GroupInput must specify name");
    }
    try {
      CreateGroup impl = createGroup.create(in.name);
      permissionBackend.user(user).checkAny(GlobalPermission.fromAnnotation(impl.getClass()));
      GroupInfo info = impl.apply(TopLevelResource.INSTANCE, in);
      return id(info.id);
    } catch (Exception e) {
      throw asRestApiException("Cannot create group " + in.name, e);
    }
  }

  @Override
  public ListRequest list() {
    return new ListRequest() {
      @Override
      public SortedMap<String, GroupInfo> getAsMap() throws RestApiException {
        return list(this);
      }
    };
  }

  private SortedMap<String, GroupInfo> list(ListRequest req) throws RestApiException {
    TopLevelResource tlr = TopLevelResource.INSTANCE;
    ListGroups list = listGroups.get();
    list.setOptions(req.getOptions());

    for (String project : req.getProjects()) {
      try {
        list.addProject(projects.parse(tlr, IdString.fromDecoded(project)).getControl());
      } catch (Exception e) {
        throw asRestApiException("Error looking up project " + project, e);
      }
    }

    for (String group : req.getGroups()) {
      list.addGroup(groups.parse(group).getGroupUUID());
    }

    list.setVisibleToAll(req.getVisibleToAll());

    if (req.getUser() != null) {
      try {
        list.setUser(accounts.parse(req.getUser()).getAccountId());
      } catch (Exception e) {
        throw asRestApiException("Error looking up user " + req.getUser(), e);
      }
    }

    list.setOwned(req.getOwned());
    list.setLimit(req.getLimit());
    list.setStart(req.getStart());
    list.setMatchSubstring(req.getSubstring());
    list.setMatchRegex(req.getRegex());
    list.setSuggest(req.getSuggest());
    try {
      return list.apply(tlr);
    } catch (Exception e) {
      throw asRestApiException("Cannot list groups", e);
    }
  }

  @Override
  public QueryRequest query() {
    return new QueryRequest() {
      @Override
      public List<GroupInfo> get() throws RestApiException {
        return GroupsImpl.this.query(this);
      }
    };
  }

  @Override
  public QueryRequest query(String query) {
    return query().withQuery(query);
  }

  private List<GroupInfo> query(QueryRequest r) throws RestApiException {
    try {
      QueryGroups myQueryGroups = queryGroups.get();
      myQueryGroups.setQuery(r.getQuery());
      myQueryGroups.setLimit(r.getLimit());
      myQueryGroups.setStart(r.getStart());
      for (ListGroupsOption option : r.getOptions()) {
        myQueryGroups.addOption(option);
      }
      return myQueryGroups.apply(TopLevelResource.INSTANCE);
    } catch (Exception e) {
      throw asRestApiException("Cannot query groups", e);
    }
  }
}