use of com.gitblit.models.RepositoryUrl in project gitblit by gitblit.
the class TicketPage method getRepositoryUrl.
/**
* Returns the primary repository url
*
* @param user
* @param repository
* @return the primary repository url
*/
protected RepositoryUrl getRepositoryUrl(UserModel user, RepositoryModel repository) {
HttpServletRequest req = ((WebRequest) getRequest()).getHttpServletRequest();
List<RepositoryUrl> urls = app().services().getRepositoryUrls(req, user, repository);
if (ArrayUtils.isEmpty(urls)) {
return null;
}
RepositoryUrl primary = urls.get(0);
return primary;
}
use of com.gitblit.models.RepositoryUrl in project gitblit by gitblit.
the class RepositoryUrlPanel method createApplicationMenus.
protected Fragment createApplicationMenus(String wicketId, final UserModel user, final RepositoryModel repository, final List<RepositoryUrl> repositoryUrls) {
final List<GitClientApplication> displayedApps = new ArrayList<GitClientApplication>();
final String userAgent = ((WebClientInfo) GitBlitWebSession.get().getClientInfo()).getUserAgent();
if (user.canClone(repository)) {
for (GitClientApplication app : app().gitblit().getClientApplications()) {
if (app.isActive && app.allowsPlatform(userAgent)) {
displayedApps.add(app);
}
}
}
final String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
ListDataProvider<GitClientApplication> displayedAppsDp = new ListDataProvider<GitClientApplication>(displayedApps);
DataView<GitClientApplication> appMenus = new DataView<GitClientApplication>("appMenus", displayedAppsDp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<GitClientApplication> item) {
final GitClientApplication clientApp = item.getModelObject();
// filter the urls for the client app
List<RepositoryUrl> urls = new ArrayList<RepositoryUrl>();
for (RepositoryUrl repoUrl : repositoryUrls) {
if (clientApp.minimumPermission == null || !repoUrl.hasPermission()) {
// no minimum permission or untracked permissions, assume it is satisfactory
if (clientApp.supportsTransport(repoUrl.url)) {
urls.add(repoUrl);
}
} else if (repoUrl.permission.atLeast(clientApp.minimumPermission)) {
// repo url meets minimum permission requirement
if (clientApp.supportsTransport(repoUrl.url)) {
urls.add(repoUrl);
}
}
}
if (urls.size() == 0) {
// do not show this app menu because there are no urls
item.add(new Label("appMenu").setVisible(false));
return;
}
Fragment appMenu = new Fragment("appMenu", "appMenuFragment", this);
appMenu.setRenderBodyOnly(true);
item.add(appMenu);
// menu button
appMenu.add(new Label("applicationName", clientApp.name));
// application icon
Component img;
if (StringUtils.isEmpty(clientApp.icon)) {
img = WicketUtils.newClearPixel("applicationIcon").setVisible(false);
} else {
if (clientApp.icon.contains("://")) {
// external image
img = new ExternalImage("applicationIcon", clientApp.icon);
} else {
// context image
img = WicketUtils.newImage("applicationIcon", clientApp.icon);
}
}
appMenu.add(img);
// application menu title, may be a link
if (StringUtils.isEmpty(clientApp.productUrl)) {
appMenu.add(new Label("applicationTitle", clientApp.toString()));
} else {
appMenu.add(new LinkPanel("applicationTitle", null, clientApp.toString(), clientApp.productUrl, true));
}
// brief application description
if (StringUtils.isEmpty(clientApp.description)) {
appMenu.add(new Label("applicationDescription").setVisible(false));
} else {
appMenu.add(new Label("applicationDescription", clientApp.description));
}
// brief application legal info, copyright, license, etc
if (StringUtils.isEmpty(clientApp.legal)) {
appMenu.add(new Label("applicationLegal").setVisible(false));
} else {
appMenu.add(new Label("applicationLegal", clientApp.legal));
}
// a nested repeater for all action items
ListDataProvider<RepositoryUrl> urlsDp = new ListDataProvider<RepositoryUrl>(urls);
DataView<RepositoryUrl> actionItems = new DataView<RepositoryUrl>("actionItems", urlsDp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<RepositoryUrl> repoLinkItem) {
RepositoryUrl repoUrl = repoLinkItem.getModelObject();
Fragment fragment = new Fragment("actionItem", "actionFragment", this);
fragment.add(createPermissionBadge("permission", repoUrl));
if (!StringUtils.isEmpty(clientApp.cloneUrl)) {
// custom registered url
String url = substitute(clientApp.cloneUrl, repoUrl.url, baseURL, user.username, repository.name);
fragment.add(new LinkPanel("content", "applicationMenuItem", getString("gb.clone") + " " + repoUrl.url, url));
repoLinkItem.add(fragment);
fragment.add(new Label("copyFunction").setVisible(false));
} else if (!StringUtils.isEmpty(clientApp.command)) {
// command-line
String command = substitute(clientApp.command, repoUrl.url, baseURL, user.username, repository.name);
Label content = new Label("content", command);
WicketUtils.setCssClass(content, "commandMenuItem");
fragment.add(content);
repoLinkItem.add(fragment);
// copy function for command
fragment.add(createCopyFragment(command));
}
}
};
appMenu.add(actionItems);
}
};
Fragment applicationMenus = new Fragment(wicketId, "applicationMenusFragment", this);
applicationMenus.add(appMenus);
return applicationMenus;
}
use of com.gitblit.models.RepositoryUrl in project gitblit by gitblit.
the class RepositoryUrlPanel method createPrimaryUrlPanel.
protected Fragment createPrimaryUrlPanel(String wicketId, final RepositoryModel repository, List<RepositoryUrl> repositoryUrls) {
Fragment urlPanel = new Fragment(wicketId, "repositoryUrlFragment", this);
urlPanel.setRenderBodyOnly(true);
if (repositoryUrls.size() == 1) {
//
// Single repository url, no dropdown menu
//
urlPanel.add(new Label("menu").setVisible(false));
} else {
//
// Multiple repository urls, show url drop down menu
//
ListDataProvider<RepositoryUrl> urlsDp = new ListDataProvider<RepositoryUrl>(repositoryUrls);
DataView<RepositoryUrl> repoUrlMenuItems = new DataView<RepositoryUrl>("repoUrls", urlsDp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<RepositoryUrl> item) {
RepositoryUrl repoUrl = item.getModelObject();
// repository url
Fragment fragment = new Fragment("repoUrl", "actionFragment", this);
Component content = new Label("content", repoUrl.url).setRenderBodyOnly(true);
WicketUtils.setCssClass(content, "commandMenuItem");
fragment.add(content);
item.add(fragment);
Label permissionLabel = new Label("permission", repoUrl.hasPermission() ? repoUrl.permission.toString() : externalPermission);
WicketUtils.setPermissionClass(permissionLabel, repoUrl.permission);
String tooltip = getProtocolPermissionDescription(repository, repoUrl);
WicketUtils.setHtmlTooltip(permissionLabel, tooltip);
fragment.add(permissionLabel);
fragment.add(createCopyFragment(repoUrl.url));
}
};
Fragment urlMenuFragment = new Fragment("menu", "urlProtocolMenuFragment", this);
urlMenuFragment.setRenderBodyOnly(true);
urlMenuFragment.add(new Label("menuText", getString("gb.url")));
urlMenuFragment.add(repoUrlMenuItems);
urlPanel.add(urlMenuFragment);
}
// access restriction icon and tooltip
if (repository.isMirror) {
urlPanel.add(WicketUtils.newImage("accessRestrictionIcon", "mirror_16x16.png", getString("gb.isMirror")));
} else if (app().services().isServingRepositories()) {
switch(repository.accessRestriction) {
case NONE:
urlPanel.add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
break;
case PUSH:
urlPanel.add(WicketUtils.newImage("accessRestrictionIcon", "lock_go_16x16.png", getAccessRestrictions().get(repository.accessRestriction)));
break;
case CLONE:
urlPanel.add(WicketUtils.newImage("accessRestrictionIcon", "lock_pull_16x16.png", getAccessRestrictions().get(repository.accessRestriction)));
break;
case VIEW:
urlPanel.add(WicketUtils.newImage("accessRestrictionIcon", "shield_16x16.png", getAccessRestrictions().get(repository.accessRestriction)));
break;
default:
if (repositoryUrls.size() == 1) {
// force left end cap to have some width
urlPanel.add(WicketUtils.newBlankIcon("accessRestrictionIcon"));
} else {
urlPanel.add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
}
}
} else {
if (repositoryUrls.size() == 1) {
// force left end cap to have some width
urlPanel.add(WicketUtils.newBlankIcon("accessRestrictionIcon"));
} else {
urlPanel.add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false));
}
}
urlPanel.add(new Label("primaryUrl", primaryUrl.url).setRenderBodyOnly(true));
Label permissionLabel = new Label("primaryUrlPermission", primaryUrl.hasPermission() ? primaryUrl.permission.toString() : externalPermission);
String tooltip = getProtocolPermissionDescription(repository, primaryUrl);
WicketUtils.setHtmlTooltip(permissionLabel, tooltip);
urlPanel.add(permissionLabel);
urlPanel.add(createCopyFragment(primaryUrl.url));
return urlPanel;
}
use of com.gitblit.models.RepositoryUrl in project gitblit by gitblit.
the class RepositoryUrlPanel method onInitialize.
@Override
protected void onInitialize() {
super.onInitialize();
HttpServletRequest req = ((WebRequest) getRequest()).getHttpServletRequest();
List<RepositoryUrl> repositoryUrls = app().services().getRepositoryUrls(req, user, repository);
// grab primary url from the top of the list
primaryUrl = repositoryUrls.size() == 0 ? null : repositoryUrls.get(0);
boolean canClone = primaryUrl != null && (!primaryUrl.hasPermission() || primaryUrl.permission.atLeast(AccessPermission.CLONE));
if (repositoryUrls.size() == 0 || !canClone) {
// no urls, nothing to show.
add(new Label("repositoryUrlPanel").setVisible(false));
add(new Label("applicationMenusPanel").setVisible(false));
add(new Label("repositoryIndicators").setVisible(false));
return;
}
// display primary url
add(createPrimaryUrlPanel("repositoryUrlPanel", repository, repositoryUrls));
if (onlyUrls) {
add(new Label("repositoryIndicators").setVisible(false));
} else {
add(createRepositoryIndicators(repository));
}
boolean allowAppLinks = app().settings().getBoolean(Keys.web.allowAppCloneLinks, true);
if (onlyUrls || !canClone || !allowAppLinks) {
// only display the url(s)
add(new Label("applicationMenusPanel").setVisible(false));
return;
}
// create the git client application menus
add(createApplicationMenus("applicationMenusPanel", user, repository, repositoryUrls));
}
use of com.gitblit.models.RepositoryUrl in project gitblit by gitblit.
the class ServicesManager method getRepositoryUrls.
/**
* Returns a list of repository URLs and the user access permission.
*
* @param request
* @param user
* @param repository
* @return a list of repository urls
*/
@Override
public List<RepositoryUrl> getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) {
if (user == null) {
user = UserModel.ANONYMOUS;
}
String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
// http/https url
if (settings.getBoolean(Keys.git.enableGitServlet, true) && settings.getBoolean(Keys.web.showHttpServletUrls, true)) {
AccessPermission permission = user.getRepositoryPermission(repository).permission;
if (permission.exceeds(AccessPermission.NONE)) {
String repoUrl = getRepositoryUrl(request, username, repository);
Transport transport = Transport.fromUrl(repoUrl);
if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(transport)) {
// downgrade the repo permission for this transport
// because it is not an acceptable PUSH transport
permission = AccessPermission.CLONE;
}
list.add(new RepositoryUrl(repoUrl, permission));
}
}
// ssh daemon url
String sshDaemonUrl = getSshDaemonUrl(request, user, repository);
if (!StringUtils.isEmpty(sshDaemonUrl) && settings.getBoolean(Keys.web.showSshDaemonUrls, true)) {
AccessPermission permission = user.getRepositoryPermission(repository).permission;
if (permission.exceeds(AccessPermission.NONE)) {
if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(Transport.SSH)) {
// downgrade the repo permission for this transport
// because it is not an acceptable PUSH transport
permission = AccessPermission.CLONE;
}
list.add(new RepositoryUrl(sshDaemonUrl, permission));
}
}
// git daemon url
String gitDaemonUrl = getGitDaemonUrl(request, user, repository);
if (!StringUtils.isEmpty(gitDaemonUrl) && settings.getBoolean(Keys.web.showGitDaemonUrls, true)) {
AccessPermission permission = getGitDaemonAccessPermission(user, repository);
if (permission.exceeds(AccessPermission.NONE)) {
if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(Transport.GIT)) {
// downgrade the repo permission for this transport
// because it is not an acceptable PUSH transport
permission = AccessPermission.CLONE;
}
list.add(new RepositoryUrl(gitDaemonUrl, permission));
}
}
// add all other urls
// {0} = repository
// {1} = username
boolean advertisePermsForOther = settings.getBoolean(Keys.web.advertiseAccessPermissionForOtherUrls, false);
for (String url : settings.getStrings(Keys.web.otherUrls)) {
String externalUrl = null;
if (url.contains("{1}")) {
// external url requires username, only add url IF we have one
if (StringUtils.isEmpty(username)) {
continue;
} else {
externalUrl = MessageFormat.format(url, repository.name, username);
}
} else {
// external url does not require username, just do repo name formatting
externalUrl = MessageFormat.format(url, repository.name);
}
AccessPermission permission = null;
if (advertisePermsForOther) {
permission = user.getRepositoryPermission(repository).permission;
if (permission.exceeds(AccessPermission.NONE)) {
Transport transport = Transport.fromUrl(externalUrl);
if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(transport)) {
// downgrade the repo permission for this transport
// because it is not an acceptable PUSH transport
permission = AccessPermission.CLONE;
}
}
}
list.add(new RepositoryUrl(externalUrl, permission));
}
// sort transports by highest permission and then by transport security
Collections.sort(list, new Comparator<RepositoryUrl>() {
@Override
public int compare(RepositoryUrl o1, RepositoryUrl o2) {
if (o1.hasPermission() && !o2.hasPermission()) {
// prefer known permission items over unknown
return -1;
} else if (!o1.hasPermission() && o2.hasPermission()) {
// prefer known permission items over unknown
return 1;
} else if (!o1.hasPermission() && !o2.hasPermission()) {
// sort by Transport ordinal
return o1.transport.compareTo(o2.transport);
} else if (o1.permission.exceeds(o2.permission)) {
// prefer highest permission
return -1;
} else if (o2.permission.exceeds(o1.permission)) {
// prefer highest permission
return 1;
}
// prefer more secure transports
return o1.transport.compareTo(o2.transport);
}
});
// consider the user's transport preference
RepositoryUrl preferredUrl = null;
Transport preferredTransport = user.getPreferences().getTransport();
if (preferredTransport != null) {
Iterator<RepositoryUrl> itr = list.iterator();
while (itr.hasNext()) {
RepositoryUrl url = itr.next();
if (url.transport.equals(preferredTransport)) {
itr.remove();
preferredUrl = url;
break;
}
}
}
if (preferredUrl != null) {
list.add(0, preferredUrl);
}
return list;
}
Aggregations