forked from nusCS2113-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Psyf/admin-mgmt
Admin management
- Loading branch information
Showing
33 changed files
with
809 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/seedu/address/logic/commands/admin/AddAdminCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package seedu.address.logic.commands.admin; | ||
|
||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.admin.Admin; | ||
import seedu.address.model.admin.Password; | ||
import seedu.address.model.admin.Username; | ||
|
||
|
||
/** | ||
* Lets one admin add another admin | ||
*/ | ||
public class AddAdminCommand extends Command { | ||
public static final String COMMAND_WORD = "addAdmin"; | ||
public static final String MESSAGE_NO_ACCESS = "You must be logged in to add another admin!"; | ||
public static final String MESSAGE_SUCCESS = "New admin added successfully!"; | ||
public static final String MESSAGE_ADMIN_ALREADY_EXISTS = "This username already exists. Try a new one or login."; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + "Used to add another admin.\n" | ||
+ "Example: addAdmin USERNAME PASSWORD PASSWORD\n"; | ||
public static final String MESSAGE_PASSWORDS_DONT_MATCH = "The two password fields don't match! Please try again."; | ||
|
||
private final Username username; | ||
private final Password password; | ||
private final Password passwordVerify; | ||
private final Admin toAddIn; | ||
|
||
public AddAdminCommand(Username username, Password password, Password passwordVerify) { | ||
this.username = username; | ||
this.password = password; | ||
this.passwordVerify = passwordVerify; | ||
this.toAddIn = new Admin(username, password); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
if (!model.isLoggedIn()) { | ||
throw new CommandException(MESSAGE_NO_ACCESS); | ||
} | ||
|
||
if (!password.equals(passwordVerify)) { | ||
throw new CommandException(MESSAGE_PASSWORDS_DONT_MATCH); | ||
} | ||
|
||
if (model.findAdmin(username) != null) { | ||
throw new CommandException(MESSAGE_ADMIN_ALREADY_EXISTS); | ||
} | ||
|
||
model.addAdmin(toAddIn); | ||
model.commitAddressBook(); //TODO: not sure what this does; | ||
|
||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof AddAdminCommand // instanceof handles nulls | ||
&& username.equals(((AddAdminCommand) other).username) | ||
&& password.equals(((AddAdminCommand) other).password) | ||
&& password.equals(((AddAdminCommand) other).passwordVerify)); // state check | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...address/logic/commands/LogoutCommand.java → ...s/logic/commands/admin/LogoutCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/address/logic/commands/admin/RemoveAdminCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package seedu.address.logic.commands.admin; | ||
|
||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.admin.Admin; | ||
import seedu.address.model.admin.Username; | ||
|
||
/** | ||
* Lets one admin remove another admin | ||
*/ | ||
public class RemoveAdminCommand extends Command { | ||
public static final String COMMAND_WORD = "removeAdmin"; | ||
public static final String MESSAGE_NO_ACCESS = "You must be logged in to remove another admin!"; | ||
public static final String MESSAGE_SUCCESS = "Admin removed successfully!"; | ||
public static final String MESSAGE_NO_SUCH_ADMIN = "No such admins exist. Have you typed the username correctly?"; | ||
private static final String MESSAGE_CANT_DELETE_LAST_ADMIN = "You can't delete the last admin."; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + "Used to remove another admin.\n" | ||
+ "Example: removeAdmin USERNAME\n"; | ||
|
||
private final Username username; | ||
|
||
public RemoveAdminCommand(Username username) { | ||
this.username = username; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
if (!model.isLoggedIn()) { | ||
throw new CommandException(MESSAGE_NO_ACCESS); | ||
} | ||
|
||
Admin toRemove = model.findAdmin(username); | ||
if (toRemove == null) { | ||
throw new CommandException(MESSAGE_NO_SUCH_ADMIN); | ||
} | ||
|
||
if (model.numAdmins() == 1) { | ||
throw new CommandException(MESSAGE_CANT_DELETE_LAST_ADMIN); | ||
} | ||
|
||
model.removeAdmin(toRemove); | ||
|
||
//Logout current admin if deleted him/herself | ||
if (model.currentlyLoggedIn().equals(username)) { | ||
model.clearLogin(); | ||
} | ||
|
||
model.commitAddressBook(); //TODO: not sure what this does; | ||
|
||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof RemoveAdminCommand // instanceof handles nulls | ||
&& username.equals(((RemoveAdminCommand) other).username)); // state check | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/seedu/address/logic/commands/admin/UpdatePasswordCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package seedu.address.logic.commands.admin; | ||
|
||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.admin.Admin; | ||
import seedu.address.model.admin.Password; | ||
import seedu.address.model.admin.Username; | ||
|
||
/** | ||
* Command used to update Password of currently logged in Admin | ||
*/ | ||
public class UpdatePasswordCommand extends Command { | ||
public static final String COMMAND_WORD = "updatePassword"; | ||
public static final String MESSAGE_NO_ACCESS = "You must be logged in to change your password!"; | ||
public static final String MESSAGE_SUCCESS = "Your password has been changed successfully!"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + " is used to change password of logged in admin.\n" | ||
+ "Example: udpatePassword USERNAME OLD_PW NEW_PW NEW_PW_VERIFY\n"; | ||
public static final String MESSAGE_ONLY_CHANGE_YOUR_OWN_PW = "You can only change your own password."; | ||
public static final String MESSAGE_PASSWORDS_DONT_MATCH = "The two password fields don't match! Please try again."; | ||
|
||
private final Username username; | ||
private final Password oldPassword; | ||
private final Password newPassword; | ||
private final Password passwordVerify; | ||
private final Admin toUpdate; | ||
private final Admin updatedAdmin; | ||
|
||
public UpdatePasswordCommand(Username username, Password oldPassword, | ||
Password newPassword, Password passwordVerify) { | ||
this.username = username; | ||
this.oldPassword = oldPassword; | ||
this.newPassword = newPassword; | ||
this.passwordVerify = passwordVerify; | ||
this.toUpdate = new Admin(username, oldPassword); | ||
this.updatedAdmin = new Admin(username, newPassword); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
if (!model.isLoggedIn()) { | ||
throw new CommandException(MESSAGE_NO_ACCESS); | ||
} | ||
|
||
if (!newPassword.equals(passwordVerify)) { | ||
throw new CommandException(MESSAGE_PASSWORDS_DONT_MATCH); | ||
} | ||
|
||
if (!username.equals(model.currentlyLoggedIn())) { | ||
throw new CommandException(MESSAGE_ONLY_CHANGE_YOUR_OWN_PW); | ||
} | ||
|
||
model.updateAdmin(toUpdate, updatedAdmin); | ||
model.commitAddressBook(); //TODO: not sure what this does; | ||
|
||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UpdatePasswordCommand // instanceof handles nulls | ||
&& username.equals(((UpdatePasswordCommand) other).username) | ||
&& newPassword.equals(((UpdatePasswordCommand) other).newPassword) | ||
&& oldPassword.equals(((UpdatePasswordCommand) other).oldPassword) | ||
&& passwordVerify.equals(((UpdatePasswordCommand) other).passwordVerify)); // state check | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.