Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #321 - simplify user vs. authentication in rendering context #324

Merged
merged 1 commit into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*/
public final class RenderingContext
implements ApplicationInfoFacade, CalendarProviderFacade, UserFacade {
/** Whether this is an identified user. */
private final boolean isUser;

/** Whether this user is an administrator. */
private final boolean isAdmin;

/** User detail object for use in rendering. */
private final User user;

Expand Down Expand Up @@ -54,8 +48,8 @@ public static RenderingContext anonymous(final ApplicationInfo appInfo,
user2.setUsername("Anonymous");
user2.setFirstname("Al");
user2.setLastname("Anonymous");
return new RenderingContext(user2, false, false, appInfo,
provider);
user2.clearRoles();
return new RenderingContext(user2, appInfo, provider);
}

/**
Expand All @@ -69,23 +63,19 @@ public static RenderingContext user(final ApplicationInfo appInfo) {
user2.setUsername("User");
user2.setFirstname("Ursula");
user2.setLastname("User");
return new RenderingContext(user2, true, false, appInfo,
new CalendarProviderStub());
user2.clearRoles();
user2.addRole("USER");
return new RenderingContext(user2, appInfo, new CalendarProviderStub());
}

/**
* Constructor with authorities.
* @param user the user detail object
* @param isUser whether it is an identified user
* @param isAdmin whether this user is an administrator
* @param appInfo provides common strings about the application
* @param calendarProvider the calendar provider to use
*/
public RenderingContext(final User user, final boolean isUser,
final boolean isAdmin, final ApplicationInfo appInfo,
public RenderingContext(final User user, final ApplicationInfo appInfo,
final CalendarProvider calendarProvider) {
this.isUser = isUser;
this.isAdmin = isAdmin;
this.user = user;
this.appInfo = appInfo;
this.calendarProvider = calendarProvider;
Expand All @@ -95,14 +85,20 @@ public RenderingContext(final User user, final boolean isUser,
* @return true if it is an identified user
*/
public boolean isUser() {
return isUser;
if (user == null) {
return false;
}
return user.hasRole("USER");
}

/**
* @return true if the user is an administrator
*/
public boolean isAdmin() {
return isAdmin;
if (user == null) {
return false;
}
return user.hasRole("ADMIN");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,14 @@ public void testHasRoleUserTrue() {
user2.setUsername("User");
user2.setFirstname("Ursula");
user2.setLastname("User");
user2.addRole("User");
user2.clearRoles();
user2.addRole("USER");
final RenderingContext context =
new RenderingContext(user2, true, false, appInfo1, provider);
new RenderingContext(user2, appInfo1, provider);
final GedRenderer<GedObject> renderer = new DefaultRenderer(root,
new GedRendererFactory(),
context);
assertTrue("Expected user to be true", renderer.hasRole("User"));
assertTrue("Expected user to be true", renderer.hasRole("USER"));
}

/** */
Expand All @@ -275,7 +276,7 @@ public void testHasRoleUserFalse() {
final GedRenderer<GedObject> renderer = new DefaultRenderer(root,
new GedRendererFactory(),
anonymousContext);
assertFalse("Expected user to be false", renderer.hasRole("User"));
assertFalse("Expected user to be false", renderer.hasRole("USER"));
}

/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ public void init() throws IOException {
admin.setUsername("admin");
admin.addRole("USER");
admin.addRole("ADMIN");
adminContext = new RenderingContext(admin, true, true, appInfo,
provider);
adminContext = new RenderingContext(admin, appInfo, provider);
final UserImpl user = new UserImpl();
user.setUsername("user");
user.addRole("USER");
userContext = new RenderingContext(user, true, false, appInfo,
provider);
userContext = new RenderingContext(user, appInfo, provider);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,9 @@ public void init() {
userContext = RenderingContext.user(appInfo);
final UserImpl user = new UserImpl();
user.setUsername("dick");
adminContext = new RenderingContext(user, true, true, appInfo,
provider);
user.addRole("USER");
user.addRole("ADMIN");
adminContext = new RenderingContext(user, appInfo, provider);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ private void createDeath(final Person person, final String placeName) {
private RenderingContext createAdminContext() {
final UserImpl user = new UserImpl();
user.setUsername("dick");
return new RenderingContext(user, true, true, appInfo, provider);
user.addRole("USER");
user.addRole("ADMIN");
return new RenderingContext(user, appInfo, provider);
}

/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ protected final RenderingContext createRenderingContext() {
final Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
final User user = users.get(authentication.getName());
final RenderingContextBuilder contextBuilder =
new RenderingContextBuilder(
authentication, user, applicationInfo, provider);
return contextBuilder.build();
return new RenderingContext(user, applicationInfo, provider);
}

/**
Expand Down

This file was deleted.