From 79dfe69726b6255464599ab852018e4d2ff96fdc Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 19 Oct 2012 22:47:35 -0400
Subject: [PATCH] Split edit repository page into tabs
---
src/com/gitblit/models/UserModel.java | 158 +++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 148 insertions(+), 10 deletions(-)
diff --git a/src/com/gitblit/models/UserModel.java b/src/com/gitblit/models/UserModel.java
index ee73025..fc9cbfb 100644
--- a/src/com/gitblit/models/UserModel.java
+++ b/src/com/gitblit/models/UserModel.java
@@ -17,15 +17,20 @@
import java.io.Serializable;
import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import com.gitblit.Constants.AccessPermission;
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.AuthorizationControl;
+import com.gitblit.Constants.RegistrantType;
import com.gitblit.Constants.Unused;
+import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.StringUtils;
/**
@@ -80,7 +85,7 @@
*/
@Deprecated
public boolean canAccessRepository(String repositoryName) {
- return canAdmin || repositories.contains(repositoryName.toLowerCase())
+ return canAdmin() || repositories.contains(repositoryName.toLowerCase())
|| hasTeamAccess(repositoryName);
}
@@ -90,7 +95,7 @@
boolean isOwner = !StringUtils.isEmpty(repository.owner)
&& repository.owner.equals(username);
boolean allowAuthenticated = isAuthenticated && AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl);
- return canAdmin || isOwner || repositories.contains(repository.name.toLowerCase())
+ return canAdmin() || isOwner || repositories.contains(repository.name.toLowerCase())
|| hasTeamAccess(repository.name) || allowAuthenticated;
}
@@ -124,6 +129,21 @@
}
/**
+ * Returns a list of repository permissions for this user exclusive of
+ * permissions inherited from team memberships.
+ *
+ * @return the user's list of permissions
+ */
+ public List<RegistrantAccessPermission> getRepositoryPermissions() {
+ List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
+ for (Map.Entry<String, AccessPermission> entry : permissions.entrySet()) {
+ list.add(new RegistrantAccessPermission(entry.getKey(), entry.getValue(), RegistrantType.REPOSITORY));
+ }
+ Collections.sort(list);
+ return list;
+ }
+
+ /**
* Returns true if the user has any type of specified access permission for
* this repository.
*
@@ -132,7 +152,21 @@
*/
public boolean hasRepositoryPermission(String name) {
String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
- return permissions.containsKey(repository) || repositories.contains(repository);
+ if (permissions.containsKey(repository)) {
+ // exact repository permission specified
+ return true;
+ } else {
+ // search for regex permission match
+ for (String key : permissions.keySet()) {
+ if (name.matches(key)) {
+ AccessPermission p = permissions.get(key);
+ if (p != null) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
}
/**
@@ -163,7 +197,7 @@
}
public AccessPermission getRepositoryPermission(RepositoryModel repository) {
- if (canAdmin || repository.isOwner(username) || repository.isUsersPersonalRepository(username)) {
+ if (canAdmin() || repository.isOwner(username) || repository.isUsersPersonalRepository(username)) {
return AccessPermission.REWIND;
}
if (AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl) && isAuthenticated) {
@@ -175,9 +209,20 @@
// and the permissions of teams of which the user belongs
AccessPermission permission = AccessPermission.NONE;
if (permissions.containsKey(repository.name.toLowerCase())) {
+ // exact repository permission specified, use it
AccessPermission p = permissions.get(repository.name.toLowerCase());
if (p != null) {
- permission = p;
+ return p;
+ }
+ } else {
+ // search for regex permission match
+ for (String key : permissions.keySet()) {
+ if (repository.name.matches(key)) {
+ AccessPermission p = permissions.get(key);
+ if (p != null) {
+ permission = p;
+ }
+ }
}
}
@@ -191,7 +236,7 @@
return permission;
}
- private boolean canAccess(RepositoryModel repository, AccessRestrictionType ifRestriction, AccessPermission requirePermission) {
+ protected boolean canAccess(RepositoryModel repository, AccessRestrictionType ifRestriction, AccessPermission requirePermission) {
if (repository.accessRestriction.atLeast(ifRestriction)) {
AccessPermission permission = getRepositoryPermission(repository);
return permission.atLeast(requirePermission);
@@ -240,24 +285,84 @@
// can not fork your own repository
return false;
}
- if (canAdmin || repository.isOwner(username)) {
+ if (canAdmin() || repository.isOwner(username)) {
return true;
}
if (!repository.allowForks) {
return false;
}
- if (!isAuthenticated || !canFork) {
+ if (!isAuthenticated || !canFork()) {
return false;
}
return canClone(repository);
}
public boolean canDelete(RepositoryModel model) {
- return canAdmin || model.isUsersPersonalRepository(username);
+ return canAdmin() || model.isUsersPersonalRepository(username);
}
public boolean canEdit(RepositoryModel model) {
- return canAdmin || model.isUsersPersonalRepository(username) || model.isOwner(username);
+ return canAdmin() || model.isUsersPersonalRepository(username) || model.isOwner(username);
+ }
+
+ /**
+ * This returns true if the user has fork privileges or the user has fork
+ * privileges because of a team membership.
+ *
+ * @return true if the user can fork
+ */
+ public boolean canFork() {
+ if (canFork) {
+ return true;
+ }
+ if (!ArrayUtils.isEmpty(teams)) {
+ for (TeamModel team : teams) {
+ if (team.canFork) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * This returns true if the user has admin privileges or the user has admin
+ * privileges because of a team membership.
+ *
+ * @return true if the user can admin
+ */
+ public boolean canAdmin() {
+ if (canAdmin) {
+ return true;
+ }
+ if (!ArrayUtils.isEmpty(teams)) {
+ for (TeamModel team : teams) {
+ if (team.canAdmin) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * This returns true if the user has create privileges or the user has create
+ * privileges because of a team membership.
+ *
+ * @return true if the user can admin
+ */
+ public boolean canCreate() {
+ if (canCreate) {
+ return true;
+ }
+ if (!ArrayUtils.isEmpty(teams)) {
+ for (TeamModel team : teams) {
+ if (team.canCreate) {
+ return true;
+ }
+ }
+ }
+ return false;
}
public boolean isTeamMember(String teamname) {
@@ -319,4 +424,37 @@
public int compareTo(UserModel o) {
return username.compareTo(o.username);
}
+
+ /**
+ * Returns true if the name/email pair match this user account.
+ *
+ * @param name
+ * @param email
+ * @return true, if the name and email address match this account
+ */
+ public boolean is(String name, String email) {
+ // at a minimum a usename or display name must be supplied
+ if (StringUtils.isEmpty(name)) {
+ return false;
+ }
+ boolean nameVerified = name.equalsIgnoreCase(username) || name.equalsIgnoreCase(getDisplayName());
+ boolean emailVerified = false;
+ if (StringUtils.isEmpty(emailAddress)) {
+ // user account has not specified an email address
+ // rely on username/displayname verification
+ emailVerified = true;
+ } else {
+ // user account has specified an email address
+ // require email address verification
+ if (!StringUtils.isEmpty(email)) {
+ emailVerified = email.equalsIgnoreCase(emailAddress);
+ }
+ }
+ return nameVerified && emailVerified;
+ }
+
+ public boolean hasBranchPermission(String repositoryName, String branch) {
+ // Default UserModel doesn't implement branch-level security. Other Realms (i.e. Gerrit) may override this method.
+ return hasRepositoryPermission(repositoryName);
+ }
}
--
Gitblit v1.9.1