Skip to content

Commit

Permalink
Merge pull request #112 from KokJianYu/EditPermission(V1.3)
Browse files Browse the repository at this point in the history
Add new command to modify the permission a user have.
  • Loading branch information
KokJianYu committed Oct 29, 2018
2 parents 3b2ee1b + de8b4d9 commit e043a3c
Show file tree
Hide file tree
Showing 39 changed files with 884 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.Permission;
import seedu.address.model.person.Person;

/**
Expand Down Expand Up @@ -46,11 +47,12 @@ public class AddCommand extends Command {
*/
public AddCommand(Person person) {
requireNonNull(person);
requiredPermission.addPermissions(Permission.ADD_EMPLOYEE);
toAdd = person;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class AutoCompleteCommandHelper {
ListCommand.COMMAND_WORD,
RedoCommand.COMMAND_WORD,
SelectCommand.COMMAND_WORD,
UndoCommand.COMMAND_WORD
UndoCommand.COMMAND_WORD,
ModifyPermissionCommand.COMMAND_WORD
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ClearCommand extends Command {


@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
requireNonNull(model);
model.resetData(new AddressBook());
model.commitAddressBook();
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,59 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.PermissionSet;
import seedu.address.model.person.User;

/**
* Represents a command with hidden internal logic and the ability to be executed.
* Set permission needed to execute the method by adding it into requiredPermission variable.
*/
public abstract class Command {

public static final String MESSAGE_LACK_REQUIRED_PERMISSION = "You do not have the permission required"
+ " to use this command.";

protected PermissionSet requiredPermission = new PermissionSet();

/**
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @param model {@code Model} which the command should operate on.
* @param history {@code CommandHistory} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
protected abstract CommandResult runBody(Model model, CommandHistory history) throws CommandException;

/**
* Check if user have required permission, then executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @param history {@code CommandHistory} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model, CommandHistory history) throws CommandException;
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
if (!haveRequiredPermission(model.getLoggedInUser())) {
throw new CommandException(MESSAGE_LACK_REQUIRED_PERMISSION);
}
return runBody(model, history);
}

/**
* Check if user have permissions required by the command.
*
* @return true if have permissions, false otherwise.
*/
public boolean haveRequiredPermission(User user) {
if (requiredPermission.getGrantedPermission().size() == 0) {
return true;
}

if (user == null) {
return false;
}

return user.getPermissionSet().containsAll(requiredPermission);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.Permission;
import seedu.address.model.person.Person;

/**
Expand All @@ -28,11 +29,12 @@ public class DeleteCommand extends Command {
private final Index targetIndex;

public DeleteCommand(Index targetIndex) {
requiredPermission.addPermissions(Permission.REMOVE_EMPLOYEE);
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.Permission;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -66,12 +67,13 @@ public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
requireNonNull(editPersonDescriptor);

requiredPermission.addPermissions(Permission.EDIT_EMPLOYEE);
this.index = index;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ExitCommand extends Command {
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";

@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
EventsCenter.getInstance().post(new ExitAppRequestEvent());
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
return new CommandResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HelpCommand extends Command {
public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
EventsCenter.getInstance().post(new ShowHelpRequestEvent());
return new CommandResult(SHOWING_HELP_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HistoryCommand extends Command {
public static final String MESSAGE_NO_HISTORY = "You have not yet entered any commands.";

@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
requireNonNull(history);
List<String> previousCommands = history.getHistory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ListCommand extends Command {


@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(MESSAGE_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LogoutCommand extends Command {
public static final String SHOWING_LOGOUT_MESSAGE = "User Logged out";

@Override
public CommandResult execute(Model model, CommandHistory history) {
public CommandResult runBody(Model model, CommandHistory history) {
EventsCenter.getInstance().post(new LogoutEvent());
return new CommandResult(SHOWING_LOGOUT_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADD_PERMISSION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMOVE_PERMISSION;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.permission.Permission;
import seedu.address.model.permission.PermissionSet;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Password;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Salary;
import seedu.address.model.person.Username;
import seedu.address.model.project.Project;

/**
* Modify the permission an employee have.
*/
public class ModifyPermissionCommand extends Command {

public static final String COMMAND_WORD = "modifypermission";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the permission of the person identified "
+ "by the index number used in the displayed person list.\n "
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_ADD_PERMISSION + " PERMISSION_TO_ADD]... "
+ "[" + PREFIX_REMOVE_PERMISSION + " PERMISSION_TO_REMOVE]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_ADD_PERMISSION + " ADD_EMPLOYEE "
+ PREFIX_REMOVE_PERMISSION + " VIEW_PROJECT";

public static final String MESSAGE_MODIFY_PERMISSION_SUCCESS = "Permission modified.\nNew Permissions : %1$s";
public static final String MESSAGE_NO_MODIFICATION = "At least 1 permission that can be added or removed "
+ "must be provided.";
public static final String MESSAGE_ADD_AND_REMOVE_SAME_PERMISSION = "Permission to be added and removed "
+ "cannot be the same.";


private Index index;
private Set<Permission> toAdd;
private Set<Permission> toRemove;

public ModifyPermissionCommand(Index index, Set<Permission> toAdd, Set<Permission> toRemove) {
requireAllNonNull(index, toAdd, toRemove);

this.requiredPermission.addPermissions(Permission.ASSIGN_PERMISSION);
this.index = index;
this.toAdd = toAdd;
this.toRemove = toRemove;
}

@Override
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
boolean isEdited;

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = duplicatePerson(personToEdit);
isEdited = editedPerson.getPermissionSet().addPermissions(toAdd);
isEdited = editedPerson.getPermissionSet().removePermissions(toRemove) || isEdited;

if (!isEdited) {
throw new CommandException(MESSAGE_NO_MODIFICATION);
}

model.updatePerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.commitAddressBook();

return new CommandResult(String.format(MESSAGE_MODIFY_PERMISSION_SUCCESS, editedPerson.getPermissionSet()));
}

/**
* Creates and return a {@code Person} with the details of {@code p}
*/
private Person duplicatePerson(Person p) {
Name name = p.getName();
Phone phone = p.getPhone();
Email email = p.getEmail();
Address address = p.getAddress();
Salary salary = p.getSalary();
Username username = p.getUsername();
Password password = p.getPassword();
Set<Project> projects = p.getProjects();
PermissionSet pSet = p.getPermissionSet();
return new Person(name, phone, email, address, salary, username, password, projects, pSet);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof ModifyPermissionCommand)) {
return false;
}

// state check
ModifyPermissionCommand mp = (ModifyPermissionCommand) other;
return index.equals(mp.index)
&& toAdd.equals(mp.toAdd)
&& toRemove.equals(mp.toRemove);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RedoCommand extends Command {
public static final String MESSAGE_FAILURE = "No more commands to redo!";

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (!model.canRedoAddressBook()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SelectCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

List<Person> filteredPersonList = model.getFilteredPersonList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class UndoCommand extends Command {
public static final String MESSAGE_FAILURE = "No more commands to undo!";

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
public CommandResult runBody(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

if (!model.canUndoAddressBook()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.LogoutCommand;
import seedu.address.logic.commands.ModifyPermissionCommand;
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.UndoCommand;
Expand Down Expand Up @@ -85,6 +86,9 @@ public Command parseCommand(String userInput) throws ParseException {
case RedoCommand.COMMAND_WORD:
return new RedoCommand();

case ModifyPermissionCommand.COMMAND_WORD:
return new ModifyPermissionCommandParser().parse(arguments);

case LogoutCommand.COMMAND_WORD:
return new LogoutCommand();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public class CliSyntax {
public static final Prefix PREFIX_PROJECT_NAME = new Prefix("-pn");
public static final Prefix PREFIX_AUTHOR = new Prefix("-au");
public static final Prefix PREFIX_PROJECT_TAG = new Prefix("-d");

/* Prefix for permission*/
public static final Prefix PREFIX_ADD_PERMISSION = new Prefix("-a");
public static final Prefix PREFIX_REMOVE_PERMISSION = new Prefix("-r");
}
Loading

0 comments on commit e043a3c

Please sign in to comment.