Skip to content

The Unified and Autonomous Development Platform

Return customized permission object to control authorization procedure for authorization optimization purpose.

robin shine0882e56389ba
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultUserManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultUserManager.java
index d34738f48b..7d2631776a 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultUserManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultUserManager.java
@@ -1,5 +1,6 @@
package com.pmease.gitop.core.manager.impl;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -30,144 +31,165 @@ import com.pmease.gitop.core.validation.UserNameReservation;
@Singleton
public class DefaultUserManager extends AbstractGenericDao<User> implements UserManager {
- private volatile Long rootUserId;
-
- private final Set<UserNameReservation> nameReservations;
-
- private final ProjectManager projectManager;
-
- @Inject
- public DefaultUserManager(GeneralDao generalDao, ProjectManager projectManager,
- Set<UserNameReservation> nameReservations) {
- super(generalDao);
-
- this.projectManager = projectManager;
- this.nameReservations = nameReservations;
- }
-
- @Sessional
- @Override
- public User getRootUser() {
- User rootUser;
- if (rootUserId == null) {
- // The first created user should be root user
- rootUser = find(null, new Order[]{Order.asc("id")});
- Preconditions.checkNotNull(rootUser);
- rootUserId = rootUser.getId();
- } else {
- rootUser = load(rootUserId);
- }
- return rootUser;
- }
-
- @Sessional
- @Override
- public User find(String userName) {
- return find(new Criterion[]{Restrictions.eq("name", userName)});
- }
-
- @Override
- public Collection<User> findPublic() {
- return query(new Criterion[]{Restrictions.eq("publiclyAccessible", true)});
- }
-
- @Override
- public EntityLoader asEntityLoader() {
- return new EntityLoader() {
-
- @Override
- public NamedEntity get(final Long id) {
- final User user = DefaultUserManager.this.get(id);
- if (user != null) {
- return new NamedEntity() {
-
- @Override
- public Long getId() {
- return id;
- }
-
- @Override
- public String getName() {
- return user.getName();
- }
-
- };
- } else {
- return null;
- }
- }
-
- @Override
- public NamedEntity get(String name) {
- final User user = find(name);
- if (user != null) {
- return new NamedEntity() {
-
- @Override
- public Long getId() {
- return user.getId();
- }
-
- @Override
- public String getName() {
- return user.getName();
- }
-
- };
- } else {
- return null;
- }
- }
-
- };
- }
-
- @Override
- public Set<String> getReservedNames() {
- Set<String> reservedNames = new HashSet<String>();
- for (UserNameReservation each: nameReservations)
- reservedNames.addAll(each.getReserved());
-
- return reservedNames;
- }
+ private volatile Long rootUserId;
+ private final Set<UserNameReservation> nameReservations;
+
+ private final ProjectManager projectManager;
+
+ @Inject
+ public DefaultUserManager(GeneralDao generalDao, ProjectManager projectManager,
+ Set<UserNameReservation> nameReservations) {
+ super(generalDao);
+
+ this.projectManager = projectManager;
+ this.nameReservations = nameReservations;
+ }
+
+ @Sessional
+ @Override
+ public User getRootUser() {
+ User rootUser;
+ if (rootUserId == null) {
+ // The first created user should be root user
+ rootUser = find(null, new Order[] {Order.asc("id")});
+ Preconditions.checkNotNull(rootUser);
+ rootUserId = rootUser.getId();
+ } else {
+ rootUser = load(rootUserId);
+ }
+ return rootUser;
+ }
+
+ @Sessional
@Override
- public Collection<Permission> permissionsOf(User user) {
- Collection<Permission> permissions = new HashSet<Permission>();
-
- if (user != null) {
- // Administrator can do anything
- if (user.isRoot() || user.isAdmin()) {
- permissions.add(ObjectPermission.ofSystemAdmin());
- return permissions;
- }
-
- // One can do anything against its belongings
- permissions.add(ObjectPermission.ofUserAdmin(user));
-
- for (Team team: user.getTeams()) {
- permissions.add(team);
+ public User find(String userName) {
+ return find(new Criterion[] {Restrictions.eq("name", userName)});
+ }
+
+ @Override
+ public Collection<User> findPublic() {
+ return query(new Criterion[] {Restrictions.eq("publiclyAccessible", true)});
+ }
+
+ @Override
+ public EntityLoader asEntityLoader() {
+ return new EntityLoader() {
+
+ @Override
+ public NamedEntity get(final Long id) {
+ final User user = DefaultUserManager.this.get(id);
+ if (user != null) {
+ return new NamedEntity() {
+
+ @Override
+ public Long getId() {
+ return id;
+ }
+
+ @Override
+ public String getName() {
+ return user.getName();
+ }
+
+ };
+ } else {
+ return null;
+ }
}
-
- for (Project each: projectManager.query()) {
- permissions.add(new ObjectPermission(each, each.getDefaultAuthorizedOperation()));
+
+ @Override
+ public NamedEntity get(String name) {
+ final User user = find(name);
+ if (user != null) {
+ return new NamedEntity() {
+
+ @Override
+ public Long getId() {
+ return user.getId();
+ }
+
+ @Override
+ public String getName() {
+ return user.getName();
+ }
+
+ };
+ } else {
+ return null;
+ }
}
- for (User each: query()) {
- permissions.add(new ObjectPermission(each, each.getDefaultAuthorizedOperation()));
+ };
+ }
+
+ @Override
+ public Set<String> getReservedNames() {
+ Set<String> reservedNames = new HashSet<String>();
+ for (UserNameReservation each : nameReservations)
+ reservedNames.addAll(each.getReserved());
+
+ return reservedNames;
+ }
+
+ @Override
+ public Collection<Permission> permissionsOf(final User user) {
+ Collection<Permission> permissions = new ArrayList<Permission>();
+
+ /*
+ * Instead of returning all permissions of the user, we return a customized
+ * permission object so that we can control the authorization procedure for
+ * optimization purpose. For instance, we may check information contained
+ * in the permission being checked and if it means authorization of certain
+ * object, we can then only load authorization information of that object.
+ */
+ permissions.add(new Permission() {
+
+ @Override
+ public boolean implies(Permission permission) {
+ // Administrator can do anything
+ if (user.isRoot() || user.isAdmin()) return true;
+
+ if (user != null) {
+ // One can do anything against its belongings
+ if (ObjectPermission.ofUserAdmin(user).implies(permission)) return true;
+
+ for (Team team : user.getTeams()) {
+ if (team.implies(permission)) return true;
+ }
+
+ for (Project each : projectManager.query()) {
+ ObjectPermission projectPermission =
+ new ObjectPermission(each, each.getDefaultAuthorizedOperation());
+ if (projectPermission.implies(permission)) return true;
+ }
+
+ for (User each : query()) {
+ ObjectPermission userPermission =
+ new ObjectPermission(each, each.getDefaultAuthorizedOperation());
+ if (userPermission.implies(permission)) return true;
+ }
+ }
+
+ // check if is public access to projects
+ for (Project each : projectManager.findPublic()) {
+ ObjectPermission projectPermission =
+ new ObjectPermission(each, GeneralOperation.READ);
+ if (projectPermission.implies(permission)) return true;
+ }
+
+ // check if is public access to accounts
+ for (User each : findPublic()) {
+ ObjectPermission userPermission =
+ new ObjectPermission(each, GeneralOperation.READ);
+ if (userPermission.implies(permission)) return true;
+ }
+
+ return false;
}
- }
-
- // check if is public access to projects
- for (Project each: projectManager.findPublic()) {
- permissions.add(new ObjectPermission(each, GeneralOperation.READ));
- }
- // check if is public access to accounts
- for (User each: findPublic()) {
- permissions.add(new ObjectPermission(each, GeneralOperation.READ));
- }
-
+ });
+
return permissions;
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java b/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
index 209c5c3117..3eec4382ec 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/model/MergeRequest.java
@@ -165,7 +165,7 @@ public class MergeRequest extends AbstractEntity {
/**
* Get list of effective updates in reverse order. Update is considered effective
* if it is descendant of merge base of latest update and target branch head.
- * <p>
+ *
* @return
* list of effective updates in reverse order.
*/