Skip to content

Commit 052b035

Browse files
committed
Add selfservice parameter on the GH team entity; don't allow users to subscribe to non-selfservice teams
1 parent 7a537c1 commit 052b035

File tree

11 files changed

+102
-29
lines changed

11 files changed

+102
-29
lines changed

src/main/java/org/jboss/set/mjolnir/client/application/admin/gitHubMembers/GitHubMembersView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected void onSelectionChanged(GithubTeam selectedTeam) {
112112
public void setData(List<GithubTeam> values) {
113113
// prepend "All teams" option
114114
values = new ArrayList<>(values);
115-
GithubTeam allTeamsItem = new GithubTeam("All teams", null);
115+
GithubTeam allTeamsItem = new GithubTeam("All teams", null, false);
116116
values.add(0, allTeamsItem);
117117
super.setData(values);
118118
}

src/main/java/org/jboss/set/mjolnir/client/application/subscriptionSetting/SubscriptionSettingView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected String getName(GithubOrganization item) {
5656

5757
@Override
5858
protected void onSelectionChanged(GithubOrganization selectedObject) {
59-
mySubscriptionsWidget.setData(selectedObject != null ? selectedObject.getTeams() : Collections.<GithubTeam>emptyList());
59+
mySubscriptionsWidget.setData(selectedObject != null ? selectedObject.getSelfServiceTeams() : Collections.<GithubTeam>emptyList());
6060
}
6161
});
6262

src/main/java/org/jboss/set/mjolnir/server/bean/GitHubSubscriptionBean.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public List<GithubOrganization> getSubscriptions(String gitHubName) {
215215
public void setSubscriptions(String gitHubName, Map<Integer, Boolean> subscriptions) {
216216
for (Map.Entry<Integer, Boolean> entry : subscriptions.entrySet()) {
217217
final Integer teamId = entry.getKey();
218+
GithubTeam team = organizationRepository.getTeam(teamId);
219+
if (team == null) {
220+
throw new ApplicationException("Managing this team subscriptions not allowed: " + teamId);
221+
}
222+
218223
if (entry.getValue()) {
219224
try {
220225
teamService.addMembership(teamId, gitHubName);
@@ -233,9 +238,14 @@ public void setSubscriptions(String gitHubName, Map<Integer, Boolean> subscripti
233238
}
234239
}
235240

236-
public void unsubscribeUser(String organization, String gitHubName) {
241+
public void unsubscribeUser(String orgName, String gitHubName) {
237242
try {
238-
organizationService.removeMember(organization, gitHubName);
243+
GithubOrganization organization = organizationRepository.getOrganization(orgName);
244+
if (organization == null) {
245+
throw new ApplicationException("Managing this organization subscriptions not allowed: " + orgName);
246+
}
247+
248+
organizationService.removeMember(orgName, gitHubName);
239249
} catch (IOException e) {
240250
throw new ApplicationException(e);
241251
}

src/main/java/org/jboss/set/mjolnir/server/bean/OrganizationRepository.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jboss.set.mjolnir.server.bean;
22

33
import org.jboss.set.mjolnir.shared.domain.GithubOrganization;
4+
import org.jboss.set.mjolnir.shared.domain.GithubTeam;
45

56
import java.util.List;
67

@@ -18,4 +19,14 @@ public interface OrganizationRepository {
1819
*/
1920
List<GithubOrganization> getOrganizations();
2021

22+
/**
23+
* Retrieves GH team with given ID.
24+
*/
25+
GithubTeam getTeam(long id);
26+
27+
/**
28+
* Retrieves GH org by name.
29+
*/
30+
GithubOrganization getOrganization(String name);
31+
2132
}

src/main/java/org/jboss/set/mjolnir/server/bean/OrganizationRepositoryBean.java

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,63 @@ public class OrganizationRepositoryBean implements OrganizationRepository {
2727

2828
@Override
2929
public List<GithubOrganization> getOrganizations() {
30-
// load organizations into a map
3130
EntityManager em = entityManagerFactory.createEntityManager();
31+
try {
32+
// load organizations into a map
33+
final List<GithubOrganizationEntity> organizationsList =
34+
em.createQuery("FROM GithubOrganizationEntity WHERE subscriptionsEnabled = true", GithubOrganizationEntity.class)
35+
.getResultList();
3236

33-
final List<GithubOrganizationEntity> organizationsList =
34-
em.createQuery("FROM GithubOrganizationEntity WHERE subscriptionsEnabled = true", GithubOrganizationEntity.class)
35-
.getResultList();
3637

37-
em.close();
38+
final Map<Long, GithubOrganization> orgMap = new HashMap<>();
3839

39-
final Map<Long, GithubOrganization> orgMap = new HashMap<>();
40-
41-
for (GithubOrganizationEntity organization : organizationsList) {
42-
final GithubOrganization org = new GithubOrganization(organization.getName());
43-
orgMap.put(organization.getId(), org);
44-
}
40+
for (GithubOrganizationEntity organization : organizationsList) {
41+
final GithubOrganization org = new GithubOrganization(organization.getName());
42+
orgMap.put(organization.getId(), org);
43+
}
4544

46-
// load teams and add them to organizations
47-
em = entityManagerFactory.createEntityManager();
45+
// load teams and add them to organizations
46+
final List<GithubTeamEntity> teamsList =
47+
em.createQuery("FROM GithubTeamEntity", GithubTeamEntity.class).getResultList();
4848

49-
final List<GithubTeamEntity> teamsList =
50-
em.createQuery("FROM GithubTeamEntity", GithubTeamEntity.class).getResultList();
49+
for (GithubTeamEntity teamEnt : teamsList) {
50+
final GithubTeam team = new GithubTeam(teamEnt.getName(), teamEnt.getGithubId().intValue(),
51+
Boolean.TRUE.equals(teamEnt.getSelfService()));
52+
final Long orgId = teamEnt.getOrganization().getId();
53+
final GithubOrganization org = orgMap.get(orgId);
54+
if (org != null) {
55+
org.addTeam(team);
56+
}
57+
}
5158

52-
em.close();
59+
return new ArrayList<>(orgMap.values());
60+
} finally {
61+
em.close();
62+
}
63+
}
5364

54-
for (GithubTeamEntity teamEnt : teamsList) {
55-
final GithubTeam team = new GithubTeam(teamEnt.getName(), teamEnt.getGithubId().intValue());
56-
final Long orgId = teamEnt.getOrganization().getId();
57-
final GithubOrganization org = orgMap.get(orgId);
58-
if (org != null) {
59-
org.addTeam(team);
60-
}
65+
@Override
66+
public GithubOrganization getOrganization(String name) {
67+
EntityManager em = entityManagerFactory.createEntityManager();
68+
try {
69+
GithubOrganizationEntity e = em.createQuery("FROM GithubOrganizationEntity WHERE name = ?1", GithubOrganizationEntity.class)
70+
.setParameter(1, name)
71+
.getSingleResult();
72+
return new GithubOrganization(e.getName());
73+
} finally {
74+
em.close();
6175
}
76+
}
6277

63-
return new ArrayList<>(orgMap.values());
78+
@Override
79+
public GithubTeam getTeam(long id) {
80+
EntityManager em = entityManagerFactory.createEntityManager();
81+
try {
82+
GithubTeamEntity e = em.find(GithubTeamEntity.class, id);
83+
return new GithubTeam(e.getName(), e.getId().intValue(), Boolean.TRUE.equals(e.getSelfService()));
84+
} finally {
85+
em.close();
86+
}
6487
}
6588

6689
}

src/main/java/org/jboss/set/mjolnir/server/entities/GithubOrganizationEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GithubOrganizationEntity {
2020
@SequenceGenerator(name = "sq_github_orgs", sequenceName = "sq_github_orgs", allocationSize = 1)
2121
private Long id;
2222

23+
@Column(name = "name")
2324
private String name;
2425

2526
@Column(name = "subscriptions_enabled")

src/main/java/org/jboss/set/mjolnir/server/entities/GithubTeamEntity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class GithubTeamEntity {
3131
@Column(name = "github_id", unique = true)
3232
private Long githubId;
3333

34+
@Column(name = "selfservice")
35+
private Boolean selfService;
36+
3437
public GithubTeamEntity() {
3538
}
3639

@@ -65,4 +68,8 @@ public Long getGithubId() {
6568
public void setGithubId(Long githubId) {
6669
this.githubId = githubId;
6770
}
71+
72+
public Boolean getSelfService() {
73+
return selfService;
74+
}
6875
}

src/main/java/org/jboss/set/mjolnir/server/service/GitHubServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public EntityUpdateResult<RegisteredUser> modifyGitHubName(String newGithubName)
123123

124124
@Override
125125
public String subscribe(int teamId) {
126+
GithubTeam team = organizationRepository.getTeam(teamId);
127+
if (team == null || !team.isSelfService()) {
128+
throw new ApplicationException("Users not allowed to manage subscription to this team: " + teamId);
129+
}
126130
final String githubName = getCurrentUserGitHubName();
127131
try {
128132
final String state = teamService.addMembership(teamId, githubName);
@@ -137,6 +141,10 @@ public String subscribe(int teamId) {
137141

138142
@Override
139143
public void unsubscribe(int teamId) {
144+
GithubTeam team = organizationRepository.getTeam(teamId);
145+
if (team == null || !team.isSelfService()) {
146+
throw new ApplicationException("Users not allowed to manage subscription to this team: " + teamId);
147+
}
140148
final String githubName = getCurrentUserGitHubName();
141149
try {
142150
teamService.removeMembership(teamId, githubName);

src/main/java/org/jboss/set/mjolnir/shared/domain/GithubOrganization.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.Serializable;
2626
import java.util.ArrayList;
2727
import java.util.List;
28+
import java.util.stream.Collectors;
2829

2930
/**
3031
* Wrapper class to hold the team information for each Github organization in the github-team-data xml file.
@@ -60,6 +61,10 @@ public List<GithubTeam> getTeams() {
6061
return teams;
6162
}
6263

64+
public List<GithubTeam> getSelfServiceTeams() {
65+
return teams.stream().filter(GithubTeam::isSelfService).collect(Collectors.toList());
66+
}
67+
6368
public String getName() {
6469
return name;
6570
}

src/main/java/org/jboss/set/mjolnir/shared/domain/GithubTeam.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public class GithubTeam implements Serializable {
3838
private String membershipState;
3939
private GithubOrganization organization;
4040

41-
public GithubTeam(String name, Integer id) {
41+
private boolean selfService;
42+
43+
public GithubTeam(String name, Integer id, boolean selfService) {
4244
this.name = name;
4345
this.id = id;
46+
this.selfService = selfService;
4447
}
4548

4649
public GithubTeam() {
@@ -70,6 +73,10 @@ public void setOrganization(GithubOrganization organization) {
7073
this.organization = organization;
7174
}
7275

76+
public boolean isSelfService() {
77+
return selfService;
78+
}
79+
7380
// GWT quirk - must implement equals method for null IDs, otherwise CellTable doesn't redraw items correctly
7481
// if an item with null id is displayed
7582
// if both object IDs are null, objects are equal

0 commit comments

Comments
 (0)