123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- /*
- * Copyright 2014 gitblit.com.
- *
- * 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.gitblit.transport.ssh.gitblit;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.kohsuke.args4j.Argument;
- import org.kohsuke.args4j.Option;
-
- import com.gitblit.Constants.AccessPermission;
- import com.gitblit.GitBlitException;
- import com.gitblit.manager.IGitblit;
- import com.gitblit.models.RegistrantAccessPermission;
- import com.gitblit.models.RepositoryModel;
- import com.gitblit.models.TeamModel;
- import com.gitblit.models.UserModel;
- import com.gitblit.transport.ssh.commands.CommandMetaData;
- import com.gitblit.transport.ssh.commands.DispatchCommand;
- import com.gitblit.transport.ssh.commands.ListFilterCommand;
- import com.gitblit.transport.ssh.commands.SshCommand;
- import com.gitblit.transport.ssh.commands.UsageExample;
- import com.gitblit.transport.ssh.commands.UsageExamples;
- import com.gitblit.utils.ArrayUtils;
- import com.gitblit.utils.FlipTable;
- import com.gitblit.utils.FlipTable.Borders;
- import com.gitblit.utils.StringUtils;
- import com.google.common.base.Joiner;
-
- @CommandMetaData(name = "teams", description = "Team management commands", admin = true)
- public class TeamsDispatcher extends DispatchCommand {
-
- @Override
- protected void setup(UserModel user) {
- // primary team commands
- register(user, NewTeam.class);
- register(user, RenameTeam.class);
- register(user, RemoveTeam.class);
- register(user, ShowTeam.class);
- register(user, ListTeams.class);
-
- // team-specific commands
- register(user, SetField.class);
- register(user, Permissions.class);
- register(user, Members.class);
- }
-
- public static abstract class TeamCommand extends SshCommand {
- @Argument(index = 0, required = true, metaVar = "TEAM", usage = "team name")
- protected String teamname;
-
- protected TeamModel getTeam(boolean requireTeam) throws UnloggedFailure {
- IGitblit gitblit = getContext().getGitblit();
- TeamModel team = gitblit.getTeamModel(teamname);
- if (requireTeam && team == null) {
- throw new UnloggedFailure(1, String.format("Team %s does not exist!", teamname));
- }
- return team;
- }
- }
-
- @CommandMetaData(name = "new", description = "Create a new team")
- @UsageExample(syntax = "${cmd} contributors --canFork --canCreate")
- public static class NewTeam extends TeamCommand {
-
- @Option(name = "--canAdmin", usage = "can administer the server")
- protected boolean canAdmin;
-
- @Option(name = "--canFork", usage = "can fork repositories")
- protected boolean canFork;
-
- @Option(name = "--canCreate", usage = "can create personal repositories")
- protected boolean canCreate;
-
- @Override
- public void run() throws UnloggedFailure {
-
- if (getTeam(false) != null) {
- throw new UnloggedFailure(1, String.format("Team %s already exists!", teamname));
- }
-
- TeamModel team = new TeamModel(teamname);
- team.canAdmin = canAdmin;
- team.canFork = canFork;
- team.canCreate = canCreate;
-
- IGitblit gitblit = getContext().getGitblit();
- try {
- gitblit.addTeam(team);
- stdout.println(String.format("%s created.", teamname));
- } catch (GitBlitException e) {
- String msg = String.format("Failed to create %s!", teamname);
- log.error(msg, e);
- throw new UnloggedFailure(1, msg);
- }
- }
- }
-
- @CommandMetaData(name = "rename", aliases = { "mv" }, description = "Rename a team")
- @UsageExample(syntax = "${cmd} contributors friends", description = "Rename the contributors team to the friends team")
- public static class RenameTeam extends TeamCommand {
- @Argument(index = 1, required = true, metaVar = "NEWNAME", usage = "the new team name")
- protected String newTeamName;
-
- @Override
- public void run() throws UnloggedFailure {
- TeamModel team = getTeam(true);
- IGitblit gitblit = getContext().getGitblit();
- if (null != gitblit.getTeamModel(newTeamName)) {
- throw new UnloggedFailure(1, String.format("Team %s already exists!", newTeamName));
- }
-
- // set the new team name
- team.name = newTeamName;
-
- try {
- gitblit.reviseTeam(teamname, team);
- stdout.println(String.format("Renamed team %s to %s.", teamname, newTeamName));
- } catch (GitBlitException e) {
- String msg = String.format("Failed to rename team from %s to %s", teamname, newTeamName);
- log.error(msg, e);
- throw new UnloggedFailure(1, msg);
- }
- }
- }
-
- @CommandMetaData(name = "set", description = "Set the specified field of a team")
- @UsageExample(syntax = "${cmd} contributors canFork true", description = "Allow the contributors team to fork repositories")
- public static class SetField extends TeamCommand {
-
- @Argument(index = 1, required = true, metaVar = "FIELD", usage = "the field to update")
- protected String fieldName;
-
- @Argument(index = 2, required = true, metaVar = "VALUE", usage = "the new value")
- protected List<String> fieldValues = new ArrayList<String>();
-
- protected enum Field {
- mailingList, canAdmin, canFork, canCreate;
-
- static Field fromString(String name) {
- for (Field field : values()) {
- if (field.name().equalsIgnoreCase(name)) {
- return field;
- }
- }
- return null;
- }
- }
-
- @Override
- protected String getUsageText() {
- String fields = Joiner.on(", ").join(Field.values());
- StringBuilder sb = new StringBuilder();
- sb.append("Valid fields are:\n ").append(fields);
- return sb.toString();
- }
-
- @Override
- public void run() throws UnloggedFailure {
- TeamModel team = getTeam(true);
-
- Field field = Field.fromString(fieldName);
- if (field == null) {
- throw new UnloggedFailure(1, String.format("Unknown field %s", fieldName));
- }
-
- String value = Joiner.on(" ").join(fieldValues);
- IGitblit gitblit = getContext().getGitblit();
-
- switch(field) {
- case mailingList:
- team.mailingLists.clear();
- team.mailingLists.addAll(fieldValues);
- break;
- case canAdmin:
- team.canAdmin = toBool(value);
- break;
- case canFork:
- team.canFork = toBool(value);
- break;
- case canCreate:
- team.canCreate = toBool(value);
- break;
- default:
- throw new UnloggedFailure(1, String.format("Field %s was not properly handled by the set command.", fieldName));
- }
-
- try {
- gitblit.reviseTeam(teamname, team);
- stdout.println(String.format("Set %s.%s = %s", teamname, fieldName, value));
- } catch (GitBlitException e) {
- String msg = String.format("Failed to set %s.%s = %s", teamname, fieldName, value);
- log.error(msg, e);
- throw new UnloggedFailure(1, msg);
- }
- }
-
- protected boolean toBool(String value) throws UnloggedFailure {
- String v = value.toLowerCase();
- if (v.equals("t")
- || v.equals("true")
- || v.equals("yes")
- || v.equals("on")
- || v.equals("y")
- || v.equals("1")) {
- return true;
- } else if (v.equals("f")
- || v.equals("false")
- || v.equals("no")
- || v.equals("off")
- || v.equals("n")
- || v.equals("0")) {
- return false;
- }
- throw new UnloggedFailure(1, String.format("Invalid boolean value %s", value));
- }
- }
-
- @CommandMetaData(name = "permissions", aliases = { "perms" }, description = "Add or remove permissions from a team")
- @UsageExample(syntax = "${cmd} contributors RW:alpha/repo.git RWC:alpha/repo2.git", description = "Add or set permissions for contributors")
- public static class Permissions extends TeamCommand {
-
- @Argument(index = 1, multiValued = true, metaVar = "[PERMISSION:]REPOSITORY", usage = "a repository expression")
- protected List<String> permissions;
-
- @Option(name = "--remove", aliases = { "-r" }, metaVar = "REPOSITORY|ALL", usage = "remove a repository permission")
- protected List<String> removals;
-
- @Override
- public void run() throws UnloggedFailure {
- IGitblit gitblit = getContext().getGitblit();
- TeamModel team = getTeam(true);
-
- boolean modified = false;
- if (!ArrayUtils.isEmpty(removals)) {
- if (removals.contains("ALL")) {
- team.permissions.clear();
- } else {
- for (String repo : removals) {
- team.removeRepositoryPermission(repo);
- log.info(String.format("Removing permission for %s from %s", repo, teamname));
- }
- }
- modified = true;
- }
-
- if (!ArrayUtils.isEmpty(permissions)) {
- for (String perm : permissions) {
- String repo = AccessPermission.repositoryFromRole(perm);
- if (StringUtils.findInvalidCharacter(repo) == null) {
- // explicit permision, confirm repository
- RepositoryModel r = gitblit.getRepositoryModel(repo);
- if (r == null) {
- throw new UnloggedFailure(1, String.format("Repository %s does not exist!", repo));
- }
- }
- AccessPermission ap = AccessPermission.permissionFromRole(perm);
- team.setRepositoryPermission(repo, ap);
- log.info(String.format("Setting %s:%s for %s", ap.name(), repo, teamname));
- }
- modified = true;
- }
-
- if (modified && gitblit.updateTeamModel(teamname, team)) {
- // reload & display new permissions
- team = gitblit.getTeamModel(teamname);
- }
-
- showPermissions(team);
- }
-
- protected void showPermissions(TeamModel team) {
- List<RegistrantAccessPermission> perms = team.getRepositoryPermissions();
- String[] pheaders = { "Repository", "Permission", "Type" };
- Object [][] pdata = new Object[perms.size()][];
- for (int i = 0; i < perms.size(); i++) {
- RegistrantAccessPermission ap = perms.get(i);
- pdata[i] = new Object[] { ap.registrant, ap.permission, ap.permissionType };
- }
- stdout.println(FlipTable.of(pheaders, pdata, Borders.BODY_HCOLS));
- }
- }
-
- @CommandMetaData(name = "members", aliases = { "users" }, description = "Add or remove team members")
- @UsageExample(syntax = "${cmd} contributors RW:alpha/repo.git RWC:alpha/repo2.git", description = "Add or set permissions for contributors")
- public static class Members extends TeamCommand {
-
- @Argument(index = 1, multiValued = true, metaVar = "USERNAME", usage = "a username")
- protected List<String> members;
-
- @Option(name = "--remove", aliases = { "-r" }, metaVar = "USERNAME|ALL", usage = "remove a team member")
- protected List<String> removals;
-
- @Override
- public void run() throws UnloggedFailure {
- IGitblit gitblit = getContext().getGitblit();
- TeamModel team = getTeam(true);
-
- boolean canEditMemberships = gitblit.supportsTeamMembershipChanges(team);
- if (!canEditMemberships) {
- String msg = String.format("Team %s (%s) does not permit membership changes!", team.name, team.accountType);
- throw new UnloggedFailure(1, msg);
- }
-
- boolean modified = false;
- if (!ArrayUtils.isEmpty(removals)) {
- if (removals.contains("ALL")) {
- team.users.clear();
- } else {
- for (String member : removals) {
- team.removeUser(member);
- log.info(String.format("Removing member %s from %s", member, teamname));
- }
- }
- modified = true;
- }
-
- if (!ArrayUtils.isEmpty(members)) {
- for (String username : members) {
- UserModel u = gitblit.getUserModel(username);
- if (u == null) {
- throw new UnloggedFailure(1, String.format("Unknown user %s", username));
- }
- boolean canEditTeams = gitblit.supportsTeamMembershipChanges(u);
- if (!canEditTeams) {
- String msg = String.format("User %s (%s) does not allow team membership changes ", u.username, u.accountType);
- throw new UnloggedFailure(1, msg);
- }
- team.addUser(username);
- }
- modified = true;
- }
-
- if (modified && gitblit.updateTeamModel(teamname, team)) {
- // reload & display new permissions
- team = gitblit.getTeamModel(teamname);
- }
-
- String[] headers = { "Username", "Display Name" };
- Object [][] data = new Object[team.users.size()][];
- int i = 0;
- for (String username : team.users) {
- UserModel u = gitblit.getUserModel(username);
- data[i] = new Object[] { username, u.displayName };
- i++;
- }
- stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS));
- }
- }
-
- @CommandMetaData(name = "remove", aliases = { "rm" }, description = "Remove a team")
- @UsageExample(syntax = "${cmd} contributors", description = "Delete the contributors team")
- public static class RemoveTeam extends TeamCommand {
-
- @Override
- public void run() throws UnloggedFailure {
-
- TeamModel team = getTeam(true);
- IGitblit gitblit = getContext().getGitblit();
- if (gitblit.deleteTeamModel(team)) {
- stdout.println(String.format("%s has been deleted.", teamname));
- } else {
- throw new UnloggedFailure(1, String.format("Failed to delete %s!", teamname));
- }
- }
- }
-
- @CommandMetaData(name = "show", description = "Show the details of a team")
- @UsageExample(syntax = "${cmd} contributors", description = "Display the 'contributors' team")
- public static class ShowTeam extends TeamCommand {
-
- @Override
- public void run() throws UnloggedFailure {
-
- TeamModel t = getTeam(true);
-
- // fields
- StringBuilder fb = new StringBuilder();
- fb.append("Mailing Lists : ").append(Joiner.on(", ").join(t.mailingLists)).append('\n');
- fb.append("Type : ").append(t.accountType).append('\n');
- fb.append("Can Admin : ").append(t.canAdmin ? "Y":"").append('\n');
- fb.append("Can Fork : ").append(t.canFork ? "Y":"").append('\n');
- fb.append("Can Create : ").append(t.canCreate ? "Y":"").append('\n');
- fb.append("Pre-Receive : ").append(Joiner.on(", ").join(t.preReceiveScripts)).append('\n');
- fb.append("Post-Receive : ").append(Joiner.on(", ").join(t.postReceiveScripts)).append('\n');
- String fields = fb.toString();
-
- // members
- String members;
- if (t.users.size() == 0) {
- members = FlipTable.EMPTY;
- } else {
- IGitblit gitblit = getContext().getGitblit();
- String[] headers = { "Username", "Display Name" };
- Object [][] data = new Object[t.users.size()][];
- int i = 0;
- for (String username : t.users) {
- UserModel u = gitblit.getUserModel(username);
- data[i] = new Object[] { username, u == null ? null : u.displayName };
- i++;
- }
- members = FlipTable.of(headers, data, Borders.COLS);
- }
-
- // permissions
- List<RegistrantAccessPermission> perms = t.getRepositoryPermissions();
- String permissions;
- if (perms.isEmpty()) {
- permissions = FlipTable.EMPTY;
- } else {
- String[] pheaders = { "Repository", "Permission", "Type" };
- Object [][] pdata = new Object[perms.size()][];
- for (int i = 0; i < perms.size(); i++) {
- RegistrantAccessPermission ap = perms.get(i);
- pdata[i] = new Object[] { ap.registrant, ap.permission, ap.permissionType };
- }
- permissions = FlipTable.of(pheaders, pdata, Borders.COLS);
- }
-
- // assemble team table
- String [] headers = new String[] { t.name };
- String[][] data = new String[6][];
- data[0] = new String [] { "FIELDS" };
- data[1] = new String [] { fields };
- data[2] = new String [] { "MEMBERS" };
- data[3] = new String [] { members };
- data[4] = new String [] { "PERMISSIONS" };
- data[5] = new String [] { permissions };
- stdout.println(FlipTable.of(headers, data));
- }
- }
-
- @CommandMetaData(name = "list", aliases= { "ls" }, description = "List teams")
- @UsageExamples(examples = {
- @UsageExample(syntax = "${cmd}", description = "List teams as a table"),
- @UsageExample(syntax = "${cmd} j.*", description = "List all teams that start with 'j'"),
- })
- public static class ListTeams extends ListFilterCommand<TeamModel> {
-
- @Override
- protected List<TeamModel> getItems() {
- IGitblit gitblit = getContext().getGitblit();
- List<TeamModel> teams = gitblit.getAllTeams();
- return teams;
- }
-
- @Override
- protected boolean matches(String filter, TeamModel t) {
- return t.name.matches(filter);
- }
-
- @Override
- protected void asTable(List<TeamModel> list) {
- String[] headers = { "Name", "Members", "Type", "Create?", "Fork?"};
- Object[][] data = new Object[list.size()][];
- for (int i = 0; i < list.size(); i++) {
- TeamModel t = list.get(i);
- data[i] = new Object[] {
- (t.canAdmin ? "*" : " ") + t.name,
- t.users.isEmpty() ? "" : t.users.size(),
- t.accountType + (t.canAdmin ? ",admin":""),
- (t.canAdmin || t.canCreate) ? "Y":"",
- (t.canAdmin || t.canFork) ? "Y" : ""};
- }
- stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS));
- }
-
- @Override
- protected void asTabbed(List<TeamModel> teams) {
- if (verbose) {
- for (TeamModel t : teams) {
- outTabbed(
- t.name,
- t.users.isEmpty() ? "" : t.users.size(),
- t.accountType + (t.canAdmin ? ",admin":""),
- (t.canAdmin || t.canCreate) ? "Y":"",
- (t.canAdmin || t.canFork) ? "Y" : "");
- }
- } else {
- for (TeamModel u : teams) {
- outTabbed((u.canAdmin ? "*" : " ") + u.name);
- }
- }
- }
- }
- }
|