Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-T09-2#137 from ncmathan/Add-lock
Browse files Browse the repository at this point in the history
Add lock, unlock, change password commands
  • Loading branch information
Mahidharah authored Nov 1, 2023
2 parents 30457aa + a369ba5 commit 4c69557
Show file tree
Hide file tree
Showing 22 changed files with 451 additions and 88 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.LockCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -41,6 +42,7 @@ public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
addressBookParser = new AddressBookParser();
new LockCommand().execute(model);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static Object format(seedu.address.model.project.Project project) {
.append(";\nDescription: ")
.append(project.getProjectDescription())
.append(";\nDeadlines:\n");
project.getProjectDeadlines().forEach(builder::append);
project.getProjectDeadlines().forEach(t -> builder.append(t.getStringRepresentation()));
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.commands;

import seedu.address.logic.parser.AddressBookParser;
import seedu.address.model.Model;
import seedu.address.model.Password;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.*;

public class ChangePasswordCommand extends Command {
public static final String COMMAND_WORD = "change-password";
public static final Object MESSAGE_USAGE = COMMAND_WORD + ": Please change password to correct format\n"
+ Password.MESSAGE_CONSTRAINTS + "\n"
+ "Example: " + COMMAND_WORD + " pw/Password123! npw/NewPass987!";
private String currentPw;
private String newPw;

public ChangePasswordCommand(String currentPw, String newPw) {
this.currentPw = currentPw;
this.newPw = newPw;
}

@Override
public CommandResult execute(Model model) {
assert model != null : "Model cannot be null";
requireNonNull(model);
String result = Password.changePassword(currentPw,newPw);
return new CommandResult(result,TabIndex.Developer);
}
}
25 changes: 25 additions & 0 deletions src/main/java/seedu/address/logic/commands/LockCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.address.logic.commands;

import seedu.address.logic.parser.AddressBookParser;
import seedu.address.model.Model;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.*;

public class LockCommand extends Command{
public static final String COMMAND_WORD = "lock";

public static final String MESSAGE_SUCCESS = "Locked all data";


@Override
public CommandResult execute(Model model) {
assert model != null : "Model cannot be null";
requireNonNull(model);
model.updateFilteredClientList(PREDICATE_SHOW_NO_CLIENT);
model.updateFilteredDeveloperList(PREDICATE_SHOW_NO_DEVELOPER);
model.updateFilteredProjectList(PREDICATE_SHOW_NO_PROJECT);
AddressBookParser.lock();
return new CommandResult(MESSAGE_SUCCESS, TabIndex.Developer);
}
}
36 changes: 36 additions & 0 deletions src/main/java/seedu/address/logic/commands/UnlockCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.logic.commands;

import seedu.address.logic.parser.AddressBookParser;
import seedu.address.model.Model;
import seedu.address.model.Password;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.*;

public class UnlockCommand extends Command {
public static final String COMMAND_WORD = "unlock";

public static final String MESSAGE_SUCCESS = "Unlocked all data";
public static final String MESSAGE_FAILURE = "Incorrect Password!\n";
public static final Object MESSAGE_USAGE = COMMAND_WORD + ": Please unlock using correct format\n"
+ "Example: " + COMMAND_WORD + " pw/Password123!";
private String input;

public UnlockCommand(String input) {
this.input = input;
}

@Override
public CommandResult execute(Model model) {
assert model != null : "Model cannot be null";
requireNonNull(model);
if (Password.verifyPassword(input)) {
model.updateFilteredClientList(PREDICATE_SHOW_ALL_CLIENTS);
model.updateFilteredDeveloperList(PREDICATE_SHOW_ALL_DEVELOPERS);
model.updateFilteredProjectList(PREDICATE_SHOW_ALL_PROJECTS);
AddressBookParser.unlock();
return new CommandResult(MESSAGE_SUCCESS, TabIndex.Developer);
}
return new CommandResult(String.format(MESSAGE_FAILURE,Password.MESSAGE_CONSTRAINTS),TabIndex.Developer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public CommandResult execute(Model model) throws CommandException {
if (model.hasClient(toAdd)) {
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_CLIENT;
}

model.addClient(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
else {
model.addClient(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
}
output += "\n";
}
return new CommandResult(output, TabIndex.Client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public CommandResult execute(Model model) throws CommandException {
if (model.hasDeveloper(toAdd)) {
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_DEVELOPER;
}

model.addDeveloper(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
else {
model.addDeveloper(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
}
output += "\n";
}
return new CommandResult(output, TabIndex.Developer);
Expand Down
116 changes: 69 additions & 47 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteDeveloperCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.*;
import seedu.address.logic.commands.find.FindClientCommand;
import seedu.address.logic.commands.find.FindDeveloperCommand;
import seedu.address.logic.commands.find.FindProjectCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.commands.list.ListClientCommand;
import seedu.address.logic.commands.list.ListDeveloperCommand;
import seedu.address.logic.commands.list.ListProjectCommand;
Expand All @@ -37,7 +30,6 @@
import seedu.address.logic.parser.edit.EditDeveloperCommandParser;
import seedu.address.logic.parser.edit.EditProjectCommandParser;
import seedu.address.logic.parser.exceptions.ParseException;

import seedu.address.logic.parser.imports.ImportClientCommandParser;
import seedu.address.logic.parser.imports.ImportDeveloperCommandParser;

Expand All @@ -55,6 +47,7 @@ public class AddressBookParser {
*/
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)");
private static final Logger logger = LogsCenter.getLogger(AddressBookParser.class);
private static boolean isLocked=true;

/**
* Parses user input into command for execution.
Expand All @@ -76,20 +69,22 @@ public Command parseCommand(String userInput) throws ParseException {
// log messages such as the one below.
// Lower level log messages are used sparingly to minimize noise in the code.
logger.fine("Command word: " + commandWord + "; Arguments: " + arguments);

switch (commandWord) {

case AddDeveloperCommand.COMMAND_WORD:
return new AddDeveloperCommandParser().parse(arguments);
case AddClientCommand.COMMAND_WORD:
return new AddClientCommandParser().parse(arguments);
case AddProjectCommand.COMMAND_WORD:
return new AddProjectCommandParser().parse(arguments);

case ImportDeveloperCommand.COMMAND_WORD:
if(isLocked==false) {
switch (commandWord) {

case AddDeveloperCommand.COMMAND_WORD:
return new AddDeveloperCommandParser().parse(arguments);
case AddClientCommand.COMMAND_WORD:
return new AddClientCommandParser().parse(arguments);
case AddProjectCommand.COMMAND_WORD:
return new AddProjectCommandParser().parse(arguments);
case ImportDeveloperCommand.COMMAND_WORD:
return new ImportDeveloperCommandParser().parse(arguments);
case ImportClientCommand.COMMAND_WORD:
return new ImportClientCommandParser().parse(arguments);

case ImportCommand.COMMAND_WORD:
return new ImportDeveloperCommandParser().parse(arguments);
case ImportClientCommand.COMMAND_WORD:
return new ImportClientCommandParser().parse(arguments);

case EditDeveloperCommand.COMMAND_WORD:
return new EditDeveloperCommandParser().parse(arguments);
Expand All @@ -101,42 +96,69 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteDeveloperCommand.COMMAND_WORD:
return new DeleteDeveloperCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindDeveloperCommand.COMMAND_WORD:
return new FindDeveloperCommandParser().parse(arguments);
case FindDeveloperCommand.COMMAND_WORD:
return new FindDeveloperCommandParser().parse(arguments);

case FindClientCommand.COMMAND_WORD:
return new FindClientCommandParser().parse(arguments);
case FindClientCommand.COMMAND_WORD:
return new FindClientCommandParser().parse(arguments);

case FindProjectCommand.COMMAND_WORD:
return new FindProjectCommandParser().parse(arguments);
case FindProjectCommand.COMMAND_WORD:
return new FindProjectCommandParser().parse(arguments);

case ListClientCommand.COMMAND_WORD:
return new ListClientCommand();
case ListClientCommand.COMMAND_WORD:
return new ListClientCommand();

case ListDeveloperCommand.COMMAND_WORD:
return new ListDeveloperCommand();
case ListDeveloperCommand.COMMAND_WORD:
return new ListDeveloperCommand();

case ListProjectCommand.COMMAND_WORD:
return new ListProjectCommand();
case ListProjectCommand.COMMAND_WORD:
return new ListProjectCommand();
case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case UndoCommand.COMMAND_WORD:
return new UndoCommand();
case RedoCommand.COMMAND_WORD:
return new RedoCommand();

case RedoCommand.COMMAND_WORD:
return new RedoCommand();
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case HelpCommand.COMMAND_WORD:
return new HelpCommand();
case LockCommand.COMMAND_WORD:
return new LockCommand();

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
case ChangePasswordCommand.COMMAND_WORD:
return new ChangePasswordCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}
else {
switch (commandWord) {
case UnlockCommand.COMMAND_WORD:
return new UnlockCommandParser().parse(arguments);
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
return new HelpCommand();

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}
}
public static void lock(){
isLocked=true;
}
public static void unlock(){
isLocked=false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.logic.parser;

import seedu.address.logic.commands.ChangePasswordCommand;
import seedu.address.logic.commands.UnlockCommand;
import seedu.address.logic.parser.exceptions.ParseException;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NEW_PASSWORD;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;

public class ChangePasswordCommandParser implements Parser<ChangePasswordCommand> {
@Override
public ChangePasswordCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_PASSWORD, PREFIX_NEW_PASSWORD);

if (argMultimap.getValue(PREFIX_PASSWORD).isPresent() && argMultimap.getValue(PREFIX_NEW_PASSWORD).isPresent()) {
String currentPw = ParserUtil.parsePassword(argMultimap.getValue(PREFIX_PASSWORD).get());
String newPw = ParserUtil.parsePassword(argMultimap.getValue(PREFIX_NEW_PASSWORD).get());
return new ChangePasswordCommand(currentPw,newPw);
}
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ChangePasswordCommand.MESSAGE_USAGE));
}
}
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 @@ -20,4 +20,8 @@ public class CliSyntax {
public static final Prefix PREFIX_DOCUMENT = new Prefix("do/");
public static final Prefix PREFIX_DESCRIPTION = new Prefix("dr/");
public static final Prefix PREFIX_DEADLINE = new Prefix("dl/");
public static final Prefix PREFIX_PASSWORD = new Prefix("pw/");
public static final Prefix PREFIX_NEW_PASSWORD = new Prefix("npw/");


}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Messages;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Password;
import seedu.address.model.client.Document;
import seedu.address.model.commons.Date;
import seedu.address.model.commons.Name;
Expand Down Expand Up @@ -251,4 +252,12 @@ public static Description parseDescription(String description) throws ParseExcep
return new Description(trimmedDescription);
}

public static String parsePassword(String pw) throws ParseException {
requireNonNull(pw);
String trimmedPw = pw.trim();
if (!Password.isValidPassword(trimmedPw)) {
throw new ParseException(Password.MESSAGE_CONSTRAINTS);
}
return trimmedPw;
}
}
23 changes: 23 additions & 0 deletions src/main/java/seedu/address/logic/parser/UnlockCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.address.logic.parser;

import seedu.address.logic.commands.UnlockCommand;
import seedu.address.logic.parser.exceptions.ParseException;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.*;

public class UnlockCommandParser implements Parser<UnlockCommand> {
@Override
public UnlockCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_PASSWORD);

if (argMultimap.getValue(PREFIX_PASSWORD).isPresent()) {
String input = ParserUtil.parsePassword(argMultimap.getValue(PREFIX_PASSWORD).get());
return new UnlockCommand(input);
}
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnlockCommand.MESSAGE_USAGE));
}
}
Loading

0 comments on commit 4c69557

Please sign in to comment.