Skip to content

Commit

Permalink
Login working and added skeletons for remaining pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoeppke committed Jun 20, 2016
1 parent 3d3cfbf commit 91d8f5c
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

import org.cocome.cloud.web.frontend.navigation.NavigationViewStates;
import org.cocome.cloud.web.login.IUser;
import org.cocome.cloud.web.login.UserRole;

public class LoginEvent {
private IUser user;
private NavigationViewStates requestedView;
private UserRole role;

public LoginEvent(@NotNull IUser user, @NotNull NavigationViewStates requestedView) {
public LoginEvent(@NotNull IUser user, @NotNull UserRole role) {
this.user = user;
this.requestedView = requestedView;
this.role = role;
}

public NavigationViewStates getRequestedView() {
return requestedView;
public UserRole getRole() {
return role;
}

public void setRequestedView(@NotNull NavigationViewStates requestedView) {
this.requestedView = requestedView;
public void setRole(@NotNull UserRole role) {
this.role = role;
}

public IUser getUser() {
Expand All @@ -30,4 +31,8 @@ public void setUser(@NotNull IUser user) {
this.user = user;
}

public NavigationViewStates getRequestedView() {
return role.associatedView();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
public class LogoutEvent {
private IUser user;

public LogoutEvent(IUser user) {
this.user = user;
}

public IUser getUser() {
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public interface INavigationElement {
* @return
*/
String getDisplayText();

/**
*
* @return
*/
String getRequiredPermission();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cocome.cloud.web.frontend.navigation;

import org.cocome.cloud.web.login.IPermission;

/**
* Represents an element inside an {@link INavigationMenu}.
*
Expand All @@ -10,6 +12,7 @@ public class NavigationElement implements INavigationElement {
private String navOutcome;
private String displayText;
private ILabelResolver resolver;
private String requiredPermission;

/**
*
Expand All @@ -18,22 +21,23 @@ public class NavigationElement implements INavigationElement {
* @param resolver
*/
public NavigationElement(NavigationElements navElement, ILabelResolver resolver) {
this(navElement.getNavigationOutcome(), null, resolver);
this(navElement.getNavigationOutcome(), null, navElement.getNeededPermission(), resolver);
}

/**
*
* @param navOutcome
* @param displayText
*/
public NavigationElement(String navOutcome, String displayText, ILabelResolver resolver) {
public NavigationElement(String navOutcome, String displayText, String requiredPermission, ILabelResolver resolver) {
this.navOutcome = navOutcome;
this.displayText = displayText;
this.resolver = resolver;
this.requiredPermission = requiredPermission;
}

public NavigationElement(NavigationElements navElement, String displayText, ILabelResolver labelResolver) {
this(navElement.getNavigationOutcome(), displayText, labelResolver);
public NavigationElement(NavigationElements navElement, String displayText, String requiredPermission, ILabelResolver labelResolver) {
this(navElement.getNavigationOutcome(), displayText, requiredPermission, labelResolver);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -70,4 +74,13 @@ public String getDisplayText() {
public void setDisplayText(String displayText) {
this.displayText = displayText;
}

@Override
public String getRequiredPermission() {
return requiredPermission;
}

public void setRequiredPermission(String requiredPermission) {
this.requiredPermission = requiredPermission;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.cocome.cloud.web.frontend.navigation;

public enum NavigationElements {
LOGOUT("logout", null),
LOGIN("login", null),
MAIN_PAGE("index", null),
MAIN_PAGE("main", null),
START_SALE("start_sale", "cashier"),
ORDER_PRODUCTS("order_products", "store manager"),
SHOW_REPORTS("show_reports", "store manager"),
ORDER_PRODUCTS("order_products", "stock manager"),
SHOW_REPORTS("show_reports", "stock manager"),
CHANGE_PRICE("change_price", "store manager"),
RECEIVE_PRODUCTS("receive_products", "stock manager"),
SHOW_ENTERPRISES("show_enterprises", "enterprise manager"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -16,6 +17,7 @@
import org.cocome.cloud.web.events.ChangeViewEvent;
import org.cocome.cloud.web.events.LoginEvent;
import org.cocome.cloud.web.events.LogoutEvent;
import org.cocome.cloud.web.login.IUser;

/**
* Implements the navigation menu for the site.
Expand All @@ -35,6 +37,8 @@ public class NavigationMenu implements INavigationMenu, Serializable {
@Inject
ILabelResolver labelResolver;

IUser currentUser;

private NavigationViewStates navigationState = NavigationViewStates.DEFAULT_VIEW;


Expand Down Expand Up @@ -64,7 +68,6 @@ private synchronized void initViewLists() {
private List<INavigationElement> populateCashpadView() {
List<INavigationElement> cashpadViewList = new LinkedList<>();
cashpadViewList.add(new NavigationElement(NavigationElements.MAIN_PAGE, labelResolver));
cashpadViewList.add(new NavigationElement(NavigationElements.LOGOUT, labelResolver));
return cashpadViewList;
}

Expand All @@ -75,7 +78,6 @@ private List<INavigationElement> populateStoreView() {
storeViewList.add(new NavigationElement(NavigationElements.SHOW_REPORTS, labelResolver));
storeViewList.add(new NavigationElement(NavigationElements.CHANGE_PRICE, labelResolver));
storeViewList.add(new NavigationElement(NavigationElements.RECEIVE_PRODUCTS, labelResolver));
storeViewList.add(new NavigationElement(NavigationElements.LOGOUT, labelResolver));
return storeViewList;
}

Expand All @@ -85,14 +87,11 @@ private List<INavigationElement> populateEnterpriseView() {
enterpriseViewList.add(new NavigationElement(NavigationElements.CREATE_ENTERPRISE, labelResolver));
enterpriseViewList.add(new NavigationElement(NavigationElements.CREATE_PRODUCT, labelResolver));
enterpriseViewList.add(new NavigationElement(NavigationElements.SHOW_PRODUCTS, labelResolver));
// TODO test this version of labels, perhaps the label resolver is not needed
enterpriseViewList.add(new NavigationElement(NavigationElements.LOGOUT, "#{strings['navigation.logout.label']}", labelResolver));
return enterpriseViewList;
}

private List<INavigationElement> populateDefaultView() {
List<INavigationElement> enterpriseViewList = new LinkedList<>();
enterpriseViewList.add(new NavigationElement(NavigationElements.LOGOUT, labelResolver));
return enterpriseViewList;
}

Expand All @@ -102,15 +101,31 @@ private List<INavigationElement> populateDefaultView() {
@Override
public List<INavigationElement> getElements() {
if (elements == null || elements.isEmpty()) {
elements = STATE_MAP.get(NavigationViewStates.DEFAULT_VIEW);
elements = new LinkedList<>(STATE_MAP.get(NavigationViewStates.DEFAULT_VIEW));
}
return elements;
}

@Override
public void changeStateTo(@NotNull NavigationViewStates newState) {
navigationState = newState;
elements = STATE_MAP.get(navigationState);
elements = new LinkedList<>(STATE_MAP.get(navigationState));

Iterator<INavigationElement> iterator = elements.iterator();

if (currentUser == null) {
navigationState = NavigationViewStates.DEFAULT_VIEW;
elements = STATE_MAP.get(NavigationViewStates.DEFAULT_VIEW);
return;
}

while (iterator.hasNext()) {
INavigationElement element = iterator.next();
if (element.getRequiredPermission() != null &&
!currentUser.hasPermission(element.getRequiredPermission())) {
iterator.remove();
}
}
}

@Override
Expand All @@ -119,10 +134,12 @@ public NavigationViewStates getCurrentState() {
}

public void observeLoginEvent(@Observes LoginEvent loginEvent) {
this.currentUser = loginEvent.getUser();
changeStateTo(loginEvent.getRequestedView());
}

public void observeLogoutEvent(@Observes LogoutEvent logoutEvent) {
this.currentUser = null;
changeStateTo(NavigationViewStates.DEFAULT_VIEW);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ private void createDummyUsers() {
ICredential cashierCredentials = new PlainCredential("cashier");

IUser admin = new DummyUser("admin", adminCredentials);
admin.addPermission(cashierPermission);
admin.addPermission(stockPermission);
admin.addPermission(storePermission);
admin.addPermission(adminPermission);
admin.addPermission(enterprisePermission);
admin.addPermission(databasePermission);
Expand All @@ -38,10 +41,12 @@ private void createDummyUsers() {

IUser storeManager = new DummyUser("storemanager", storeCredentials);
storeManager.addPermission(storePermission);
storeManager.addPermission(stockPermission);
storeManager.addPermission(cashierPermission);

IUser stockManager = new DummyUser("stockmanager", storeCredentials);
stockManager.addPermission(storePermission);
stockManager.addPermission(stockPermission);
stockManager.addPermission(cashierPermission);

IUser cashier = new DummyUser("cashier", cashierCredentials);
cashier.addPermission(cashierPermission);
Expand Down Expand Up @@ -85,8 +90,10 @@ public IUser checkCredential(String username, ICredential credential) {
if(storedUser.checkCredentials(credential)) {
return storedUser;
}
LOG.debug("Wrong credentials provided.");
} else {
LOG.warn("No user with name " + username + " found!");
}
LOG.warn("No user with name " + username + " found!");
}
return null;
}
Expand Down
Loading

0 comments on commit 91d8f5c

Please sign in to comment.