Skip to content
Snippets Groups Projects
Commit b15476e6 authored by dmz39's avatar dmz39
Browse files

Adding new groups functionality that allows permissions to be set for users by linking groups

parent 36d8d556
Branches
No related tags found
No related merge requests found
Showing
with 159 additions and 33 deletions
INSERT INTO groups (group_name, group_type) VALUES ('DEFAULT', 'CUSTOMER');
INSERT INTO groups (group_name, group_type) VALUES ('Admin', 'ADMIN');
INSERT INTO groups (group_name, group_type) VALUES ('Employee', 'EMPLOYEE');
\ No newline at end of file
...@@ -4,8 +4,8 @@ CREATE TABLE user_entity ( ...@@ -4,8 +4,8 @@ CREATE TABLE user_entity (
first_name TEXT NOT NULL, first_name TEXT NOT NULL,
last_name TEXT NOT NULL, last_name TEXT NOT NULL,
phone_number TEXT NOT NULL, phone_number TEXT NOT NULL,
is_admin BOOLEAN NOT NULL, external_id BIGINT,
external_id BIGINT) group_id BIGINT NOT NULL)
WITH (OIDS = FALSE); WITH (OIDS = FALSE);
CREATE TABLE address ( CREATE TABLE address (
...@@ -19,3 +19,9 @@ CREATE TABLE address ( ...@@ -19,3 +19,9 @@ CREATE TABLE address (
user_id BIGINT NOT NULL, user_id BIGINT NOT NULL,
is_billing BOOLEAN NOT NULL) is_billing BOOLEAN NOT NULL)
WITH (OIDS = FALSE); WITH (OIDS = FALSE);
CREATE TABLE groups (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
group_name TEXT NOT NULL,
group_type VARCHAR(8) NOT NULL)
WITH (OIDS = FALSE);
\ No newline at end of file
...@@ -10,6 +10,9 @@ import org.springframework.security.oauth2.core.user.OAuth2User; ...@@ -10,6 +10,9 @@ import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List; import java.util.List;
...@@ -25,7 +28,7 @@ public class AdminController { ...@@ -25,7 +28,7 @@ public class AdminController {
@GetMapping("/admin/dashboard") @GetMapping("/admin/dashboard")
String adminDashboard(@AuthenticationPrincipal OAuth2User principal) { String adminDashboard(@AuthenticationPrincipal OAuth2User principal) {
UserEntity currentUser = this.userService.getUser(principal); UserEntity currentUser = this.userService.getUser(principal);
if (currentUser != null && currentUser.getIsAdmin()) { if (currentUser != null && this.userService.isAllowedAdminPanel(currentUser)) {
return "admin/dashboard"; return "admin/dashboard";
} else { } else {
return "admin/access_denied"; return "admin/access_denied";
...@@ -35,19 +38,28 @@ public class AdminController { ...@@ -35,19 +38,28 @@ public class AdminController {
@GetMapping("admin/users") @GetMapping("admin/users")
String manageUsers(@AuthenticationPrincipal OAuth2User principal, Model model) { String manageUsers(@AuthenticationPrincipal OAuth2User principal, Model model) {
UserEntity currentUser = this.userService.getUser(principal); UserEntity currentUser = this.userService.getUser(principal);
if (currentUser != null && currentUser.getIsAdmin()) { if (currentUser != null && this.userService.isAllowedAdminPanel(currentUser)) {
List<UserEntity> users = this.userService.getUsers(); List<UserEntity> users = this.userService.getUsers();
model.addAttribute("users", users); model.addAttribute("users", users);
model.addAttribute("groups", this.groupService.getGroups());
return "admin/users"; return "admin/users";
} else { } else {
return "admin/access_denied"; return "admin/access_denied";
} }
} }
@RequestMapping("admin/saveUser")
@ResponseBody
void saveUser(@RequestParam long userExternalId, @RequestParam String firstName, @RequestParam String lastName, @RequestParam String phoneNumber, @RequestParam Long groupId) {
UserEntity oldUser = this.userService.getUser(userExternalId);
UserEntity newUser = new UserEntity(firstName, lastName, phoneNumber, userExternalId, groupId);
this.userService.saveUser(oldUser, newUser);
}
@GetMapping("admin/groups") @GetMapping("admin/groups")
String manageGroups(@AuthenticationPrincipal OAuth2User principal, Model model) { String manageGroups(@AuthenticationPrincipal OAuth2User principal, Model model) {
UserEntity currentUser = this.userService.getUser(principal); UserEntity currentUser = this.userService.getUser(principal);
if (currentUser != null && currentUser.getIsAdmin()) { if (currentUser != null && this.userService.isAllowedAdminPanel(currentUser)) {
List<Group> groups = this.groupService.getGroups(); List<Group> groups = this.groupService.getGroups();
model.addAttribute("groups", groups); model.addAttribute("groups", groups);
return "admin/groups"; return "admin/groups";
... ...
......
package edu.drexel.TrainDemo.controllers.users; package edu.drexel.TrainDemo.controllers.users;
import edu.drexel.TrainDemo.models.users.Address; import edu.drexel.TrainDemo.models.users.Address;
import edu.drexel.TrainDemo.services.users.GroupService;
import edu.drexel.TrainDemo.services.users.UserService; import edu.drexel.TrainDemo.services.users.UserService;
import edu.drexel.TrainDemo.Utils; import edu.drexel.TrainDemo.Utils;
import edu.drexel.TrainDemo.models.users.UserEntity; import edu.drexel.TrainDemo.models.users.UserEntity;
...@@ -25,6 +26,9 @@ public class UserController { ...@@ -25,6 +26,9 @@ public class UserController {
@Autowired @Autowired
UserService userService; UserService userService;
@Autowired
GroupService groupService;
@GetMapping("/user") @GetMapping("/user")
@ResponseBody @ResponseBody
public Map<String, Object> user(@AuthenticationPrincipal OAuth2User principal) { public Map<String, Object> user(@AuthenticationPrincipal OAuth2User principal) {
...@@ -42,9 +46,11 @@ public class UserController { ...@@ -42,9 +46,11 @@ public class UserController {
@RequestMapping("/user/dashboard") @RequestMapping("/user/dashboard")
String userDashboard(@AuthenticationPrincipal OAuth2User principal, Model model) { String userDashboard(@AuthenticationPrincipal OAuth2User principal, Model model) {
UserEntity currentUser = this.userService.getUser(Utils.intToLong(principal.getAttribute("id"))); UserEntity currentUser = this.userService.getUser(Utils.intToLong(principal.getAttribute("id")));
String groupName = this.groupService.findNameById(currentUser.getGroupId());
model.addAttribute("firstName", currentUser.getFirstName()); model.addAttribute("firstName", currentUser.getFirstName());
model.addAttribute("lastName", currentUser.getLastName()); model.addAttribute("lastName", currentUser.getLastName());
model.addAttribute("phoneNumber", currentUser.getPhoneNumber()); model.addAttribute("phoneNumber", currentUser.getPhoneNumber());
model.addAttribute("groupName", groupName);
return "user/dashboard"; return "user/dashboard";
} }
... ...
......
...@@ -24,8 +24,8 @@ public class UserEntity implements Serializable { ...@@ -24,8 +24,8 @@ public class UserEntity implements Serializable {
// TODO: Add email only if needed? Would need to update database tables // TODO: Add email only if needed? Would need to update database tables
@Column(name = "is_admin") @Column(name = "group_id")
private Boolean isAdmin; private Long groupId;
@Column(name = "external_id") @Column(name = "external_id")
private Long externalId; private Long externalId;
...@@ -37,7 +37,15 @@ public class UserEntity implements Serializable { ...@@ -37,7 +37,15 @@ public class UserEntity implements Serializable {
this.lastName = lastName; this.lastName = lastName;
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
this.externalId = clientid; this.externalId = clientid;
this.isAdmin = false; this.groupId = null;
}
public UserEntity(String firstName, String lastName, String phoneNumber, long clientid, Long groupId) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.externalId = clientid;
this.groupId = groupId;
} }
public Long getId() { public Long getId() {
...@@ -54,8 +62,8 @@ public class UserEntity implements Serializable { ...@@ -54,8 +62,8 @@ public class UserEntity implements Serializable {
public String getPhoneNumber() { return this.phoneNumber; } public String getPhoneNumber() { return this.phoneNumber; }
public boolean getIsAdmin() { public Long getGroupId() {
return this.isAdmin; return this.groupId;
} }
public Long getExternalId() { public Long getExternalId() {
...@@ -74,16 +82,8 @@ public class UserEntity implements Serializable { ...@@ -74,16 +82,8 @@ public class UserEntity implements Serializable {
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
} }
public void setAdmin(Boolean admin) { public void setGroupId(Long groupId) {
isAdmin = admin; this.groupId = groupId;
}
/**
* This function is most likely only used during development and eval for looking at admin panels.
* @param shouldBeAdmin
*/
public void setIsAdmin(Boolean shouldBeAdmin) {
this.isAdmin = shouldBeAdmin;
} }
public String toString() { public String toString() {
... ...
......
...@@ -11,7 +11,7 @@ public interface GroupRepository extends CrudRepository<Group, String> ...@@ -11,7 +11,7 @@ public interface GroupRepository extends CrudRepository<Group, String>
{ {
List<Group> findAll(); List<Group> findAll();
// List<Group> findByIds(Integer groupId); Group findGroupByGroupId(Long groupId);
// List<Group> findByNames(String groupName); // List<Group> findByNames(String groupName);
// List<Group> findByType(GroupType groupType); // List<Group> findByType(GroupType groupType);
Group findByGroupName(String name); Group findByGroupName(String name);
... ...
......
...@@ -15,6 +15,8 @@ public interface GroupService ...@@ -15,6 +15,8 @@ public interface GroupService
// List<Group> listGroupByNames(List<String> names); // List<Group> listGroupByNames(List<String> names);
String findNameById(Long groupid);
Group findByName(String groupName); Group findByName(String groupName);
void createGroup(String groupName, GroupType groupType); void createGroup(String groupName, GroupType groupType);
... ...
......
...@@ -43,6 +43,11 @@ public class GroupServiceImpl implements GroupService ...@@ -43,6 +43,11 @@ public class GroupServiceImpl implements GroupService
// return groupRepository.findByNames(name); // return groupRepository.findByNames(name);
// } // }
public String findNameById(Long groupid) {
Group group = this.groupRepository.findGroupByGroupId(groupid);
return group.getGroupName();
}
public Group findByName(String groupName) public Group findByName(String groupName)
{ {
return groupRepository.findByGroupName(groupName); return groupRepository.findByGroupName(groupName);
... ...
......
...@@ -18,6 +18,9 @@ public interface UserService { ...@@ -18,6 +18,9 @@ public interface UserService {
void saveUser(UserEntity user, UserEntity newUser); void saveUser(UserEntity user, UserEntity newUser);
void removeUser(OAuth2User principal); void removeUser(OAuth2User principal);
boolean isAllowedAdminPanel(UserEntity userEntity);
void changeGroupId(Long userid, Long groupId);
// Address Functions // Address Functions
List<Address> getBillingAddresses(OAuth2User principal); List<Address> getBillingAddresses(OAuth2User principal);
//List<Address> getBillingAddresses(long userid); //List<Address> getBillingAddresses(long userid);
... ...
......
...@@ -2,10 +2,14 @@ package edu.drexel.TrainDemo.services.users; ...@@ -2,10 +2,14 @@ package edu.drexel.TrainDemo.services.users;
import edu.drexel.TrainDemo.Utils; import edu.drexel.TrainDemo.Utils;
import edu.drexel.TrainDemo.models.users.Address; import edu.drexel.TrainDemo.models.users.Address;
import edu.drexel.TrainDemo.models.users.Group;
import edu.drexel.TrainDemo.models.users.GroupType;
import edu.drexel.TrainDemo.models.users.UserEntity; import edu.drexel.TrainDemo.models.users.UserEntity;
import edu.drexel.TrainDemo.repositories.users.AddressRepository; import edu.drexel.TrainDemo.repositories.users.AddressRepository;
import edu.drexel.TrainDemo.repositories.users.GroupRepository;
import edu.drexel.TrainDemo.repositories.users.UserRepository; import edu.drexel.TrainDemo.repositories.users.UserRepository;
import org.apache.catalina.User;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.oauth2.core.user.OAuth2User;
...@@ -26,6 +30,9 @@ public class UserServiceImpl implements UserService { ...@@ -26,6 +30,9 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private AddressRepository addressRepo; private AddressRepository addressRepo;
@Autowired
private GroupRepository groupRepo;
@Override @Override
public List<UserEntity> getUsers() { public List<UserEntity> getUsers() {
List<UserEntity> allUsers = new ArrayList<>(); List<UserEntity> allUsers = new ArrayList<>();
...@@ -55,7 +62,8 @@ public class UserServiceImpl implements UserService { ...@@ -55,7 +62,8 @@ public class UserServiceImpl implements UserService {
@Override @Override
public UserEntity createUser(long id, String firstName, String lastName, String phoneNumber) { public UserEntity createUser(long id, String firstName, String lastName, String phoneNumber) {
UserEntity newUser = new UserEntity(firstName, lastName, phoneNumber, id); Long groupid = this.groupRepo.findByGroupName("DEFAULT").getGroupId();
UserEntity newUser = new UserEntity(firstName, lastName, phoneNumber, id, groupid);
System.out.println("createUser: " + newUser.getExternalId() + " | " + newUser.getFirstName() + " | " + newUser.getLastName()); System.out.println("createUser: " + newUser.getExternalId() + " | " + newUser.getFirstName() + " | " + newUser.getLastName());
this.userRepo.save(newUser); this.userRepo.save(newUser);
getUser(id); getUser(id);
...@@ -74,11 +82,27 @@ public class UserServiceImpl implements UserService { ...@@ -74,11 +82,27 @@ public class UserServiceImpl implements UserService {
oldUser.setFirstName(newUser.getFirstName()); oldUser.setFirstName(newUser.getFirstName());
oldUser.setLastName(newUser.getLastName()); oldUser.setLastName(newUser.getLastName());
oldUser.setPhoneNumber(newUser.getPhoneNumber()); oldUser.setPhoneNumber(newUser.getPhoneNumber());
oldUser.setIsAdmin(newUser.getIsAdmin()); if (newUser.getGroupId() != null) {
oldUser.setGroupId(newUser.getGroupId());
}
System.out.println(oldUser.toString()); System.out.println(oldUser.toString());
this.userRepo.save(oldUser); this.userRepo.save(oldUser);
} }
@Override
public boolean isAllowedAdminPanel(UserEntity user) {
Group group = this.groupRepo.findGroupByGroupId(user.getGroupId());
System.out.println(group.getGroupType());
return (group.getGroupType() == GroupType.ADMIN || group.getGroupType() == GroupType.EMPLOYEE);
}
@Override
public void changeGroupId(Long userid, Long groupId) {
UserEntity user = this.getUser(userid);
user.setGroupId(groupId);
this.userRepo.save(user);
}
@Override @Override
public void removeUser(OAuth2User principal) { public void removeUser(OAuth2User principal) {
this.userRepo.delete(getUser(principal)); this.userRepo.delete(getUser(principal));
... ...
......
...@@ -8,6 +8,27 @@ function showModal(modalid) { ...@@ -8,6 +8,27 @@ function showModal(modalid) {
$('#' + modalid).modal("show"); $('#' + modalid).modal("show");
} }
function adminEditUser(userid) {
var data = $("#"+userid).find("#userGroup").html();
console.log(data);
$("#editUserModal").find('#externalId').val($("#"+userid).find("#userExternalId").html());
$("#editUserModal").find('#fname').val($("#"+userid).find("#userFirstName").html());
$("#editUserModal").find('#lname').val($("#"+userid).find("#userLastName").html());
$("#editUserModal").find('#phNumber').val($("#"+userid).find("#userPhNumber").html());
$("#editUserModal").find('#group').children("option[value=" + data + "]").attr('selected', 'selected');
$("#editUserModal").modal("show");
}
function adminSaveUser() {
$.post("/admin/saveUser", {
userExternalId: $("#editUserModal").find('#externalId').val(),
firstName: $("#editUserModal").find('#fname').val(),
lastName: $("#editUserModal").find('#lname').val(),
phoneNumber: $("#editUserModal").find('#phNumber').val(),
groupId: $("#editUserModal").find('#group').children("option:selected").val()
});
}
function addGroup() { function addGroup() {
var groupName = $('#newGroupModal').find('#gname').val(); var groupName = $('#newGroupModal').find('#gname').val();
var groupType = $('#newGroupModal').find('#gtype').children("option:selected").val(); var groupType = $('#newGroupModal').find('#gtype').children("option:selected").val();
... ...
......
...@@ -34,20 +34,20 @@ ...@@ -34,20 +34,20 @@
<th class="th-sm">First Name</th> <th class="th-sm">First Name</th>
<th class="th-sm">Last Name</th> <th class="th-sm">Last Name</th>
<th class="th-sm">Phone #</th> <th class="th-sm">Phone #</th>
<th class="th-sm">Admin</th> <th class="th-sm">Group</th>
<th class="th-sm">Details</th> <th class="th-sm">Details</th>
<th class="th-sm">Delete</th> <th class="th-sm">Delete</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="user : ${users}"> <tr th:each="user,iter : ${users}" th:id="${iter.index + 1}">
<td th:text="${user.getId()}"></td> <td id="userId" th:text="${user.getId()}"></td>
<td th:text="${user.getExternalId()}"></td> <td id="userExternalId" th:text="${user.getExternalId()}"></td>
<td th:text="${user.getFirstName()}"></td> <td id="userFirstName" th:text="${user.getFirstName()}"></td>
<td th:text="${user.getLastName()}"></td> <td id="userLastName" th:text="${user.getLastName()}"></td>
<td th:text="${user.getPhoneNumber()}"></td> <td id="userPhNumber" th:text="${user.getPhoneNumber()}"></td>
<td th:text="${user.getIsAdmin()}"></td> <td id="userGroup" th:data="${user.getGroupId()}" th:text="${user.getGroupId()}"></td>
<td><button class="btn btn-primary" th:type="button">Details</button></td> <td><button class="btn btn-primary" th:type="button" th:onclick="'adminEditUser(' + ${iter.index + 1} + ')'">Details</button></td>
<td><button class="btn btn-primary" th:type="button">&times;</button></td> <td><button class="btn btn-primary" th:type="button">&times;</button></td>
</tr> </tr>
</tbody> </tbody>
...@@ -55,6 +55,49 @@ ...@@ -55,6 +55,49 @@
</div> </div>
</div> </div>
</div> </div>
<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" id="editUserModal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editUserModalLongTitle">Edit User Account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="modal-body">
<p>Please Update This User's Info:</p>
<a id="externalId"></a>
<form>
<div class="form-group ">
<label for="fname">First Name:</label>
<input type="text" class="form-control" id="fname">
</div>
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" class="form-control" id="lname">
</div>
<div class="form-group">
<label for="phNumber">Phone Number: ex: (9145536614)</label>
<input type="text" class="form-control" id="phNumber" pattern="[0-9]*">
</div>
<div class="form-group">
<label for="group">Group:</label>
<select type="name" class="form-control" id="group">
<option value="">-- Group --</option>
<option th:each="group : ${groups}" th:value="${group.getGroupId()}" th:text="${group.getGroupName() + ' (' + group.getGroupType() + ')'}"></option>
</select>
</div>
<div class="form-group">
<a th:href="@{''}"><button type="button" class="btn btn-primary" onclick="adminSaveUser()">Save</button></a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main> </main>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
<p> <p>
<a class="text">User Account Info:</a><br><br> <a class="text">User Account Info:</a><br><br>
<pre><a class="text">Name: </a><a class="text" th:text="${firstName}"></a> <a class="text" th:text="${lastName}"></a></pre><br> <pre><a class="text">Name: </a><a class="text" th:text="${firstName}"></a> <a class="text" th:text="${lastName}"></a></pre><br>
<pre><a class="text">Phone Number: </a><a class="text" th:text="${phoneNumber}"></a></pre><br><br> <pre><a class="text">Phone Number: </a><a class="text" th:text="${phoneNumber}"></a></pre><br>
<pre><a class="text">Group Name: </a><a class="text" th:text="${groupName}"></a></pre><br><br>
<a><button class="btn btn-primary" onclick="showEditModal()">Edit Account Details</button></a> <a><button class="btn btn-primary" onclick="showEditModal()">Edit Account Details</button></a>
</p> </p>
</div> </div>
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment