Roles y User Completos:
-End Completo. -Falta conectar Roles con Access
This commit is contained in:
parent
ca8dae036b
commit
9382fa9ceb
@ -5,7 +5,6 @@ import com.google.appengine.api.datastore.KeyFactory;
|
||||
import controller.users.UsersControllerView;
|
||||
import model.Role;
|
||||
|
||||
import javax.jdo.JDOObjectNotFoundException;
|
||||
import javax.jdo.PersistenceManager;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
@ -47,13 +46,7 @@ public class RolesControllerAdd extends HttpServlet {
|
||||
String name = request.getParameter("roleName");
|
||||
Boolean status = Boolean.parseBoolean(request.getParameter("roleStatus"));
|
||||
|
||||
Role role = new Role(name,status);
|
||||
|
||||
try{
|
||||
pm.makePersistent(role);
|
||||
} finally {
|
||||
System.out.println("Role creado");
|
||||
}
|
||||
createRole(name,status,pm);
|
||||
|
||||
break;
|
||||
|
||||
@ -94,5 +87,15 @@ public class RolesControllerAdd extends HttpServlet {
|
||||
doPost(request, response);
|
||||
}
|
||||
|
||||
public static String createRole(String name, boolean status, PersistenceManager pm){
|
||||
Role role = new Role(name,status);
|
||||
|
||||
try{
|
||||
pm.makePersistent(role);
|
||||
return role.getKey();
|
||||
} finally {
|
||||
System.out.println("Role creado");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -108,4 +108,12 @@ public class RolesControllerView extends HttpServlet {
|
||||
pm.close();
|
||||
return role;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Role> searchRole(String name){
|
||||
PersistenceManager pm = controller.PMF.get().getPersistenceManager();
|
||||
List<Role> users = (List<Role>) pm.newQuery("select from " + Role.class.getName() + " where name == '" + name + "'").execute();
|
||||
pm.close();
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package controller.users;
|
||||
|
||||
import controller.roles.RolesControllerAdd;
|
||||
import controller.roles.RolesControllerView;
|
||||
import model.Role;
|
||||
import model.User;
|
||||
|
||||
@ -12,6 +14,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class UsersControllerAdd extends HttpServlet {
|
||||
@ -53,14 +56,21 @@ public class UsersControllerAdd extends HttpServlet {
|
||||
//Busca si ya existe una sesion iniciada
|
||||
HttpSession misesion = request.getSession();
|
||||
|
||||
crearUsuario(userID, userEmail, userName, userImg, userRole, pm);
|
||||
List<Role> roleList = RolesControllerView.searchRole(userRole);
|
||||
|
||||
if (roleList.size() > 0){
|
||||
userRole = roleList.get(0).getKey();
|
||||
} else {
|
||||
userRole = RolesControllerAdd.createRole(userRole,true,pm);
|
||||
}
|
||||
|
||||
createUser(userID, userEmail, userName, userImg, userRole, pm);
|
||||
|
||||
//Si no existe la sesion, la crea usando el ID del usuario
|
||||
if (!sesionExist(misesion)) {
|
||||
|
||||
misesion = request.getSession(true);
|
||||
misesion.setAttribute("userID", userID);
|
||||
System.out.println("Sesiones: luego de add -> " + misesion.getAttribute("userID"));
|
||||
|
||||
//La sesion perdurara sin actividad durante 1h.
|
||||
misesion.setMaxInactiveInterval(3600);
|
||||
@ -73,12 +83,13 @@ public class UsersControllerAdd extends HttpServlet {
|
||||
HttpSession sesion= request.getSession();
|
||||
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/View/Users/Add.jsp");
|
||||
request.setAttribute("User",UsersControllerView.getUser(sesion.getAttribute("userID").toString()));
|
||||
request.setAttribute("Roles",RolesControllerView.getAllRoles());
|
||||
dispatcher.forward(request, response);
|
||||
break;
|
||||
|
||||
//Si lo que se quiere es Crear (proviene del formulario)
|
||||
case "create":
|
||||
crearUsuario(userID, userEmail, userName, userImg, userRole, pm);
|
||||
createUser(userID, userEmail, userName, userImg, userRole, pm);
|
||||
break;
|
||||
|
||||
//Si lo que se quiere es actualizar un Usuario
|
||||
@ -89,7 +100,7 @@ public class UsersControllerAdd extends HttpServlet {
|
||||
user.setName(userName);
|
||||
user.setEmail(userEmail);
|
||||
user.setImgUrl(userImg);
|
||||
user.setRole(new Role(userRole,true));
|
||||
user.setRole(userRole);
|
||||
|
||||
break;
|
||||
|
||||
@ -141,14 +152,14 @@ public class UsersControllerAdd extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
private void crearUsuario(String userID, String userEmail, String userName, String userImg, String userRole, PersistenceManager pm){
|
||||
private void createUser(String userID, String userEmail, String userName, String userImg, String userRole, PersistenceManager pm){
|
||||
|
||||
//Revisa si el usuario con su ID ya tiene un objeto User Persistente almacenado.
|
||||
//Si no existe, crea el objeto de tipo User con los datos que se obtienen del request, y lo hace Persistente.
|
||||
if (!userExists(userID, pm)){
|
||||
|
||||
//El new Role es provisional, hasta que termine la implementacion del CRUD de Role.
|
||||
User user = new User(userID, userName, userImg, userEmail, new Role(userRole,true));
|
||||
User user = new User(userID, userName, userImg, userEmail, userRole);
|
||||
|
||||
try{
|
||||
pm.makePersistent(user);
|
||||
|
@ -33,8 +33,8 @@ public class UsersControllerIndex extends HttpServlet {
|
||||
}
|
||||
//Si no la encuentra, redirige a la pagina inicial.
|
||||
catch (Exception e){
|
||||
System.err.println("Error catched. " + e.getMessage());
|
||||
response.getWriter().println("<html><head><script>window.location.replace(\"../\")</script></head><body></bodyy></html>");
|
||||
System.err.println("UserControllerIndex: Error catched. " + e.getMessage());
|
||||
response.getWriter().println("<html><head><script>window.location.replace(\"../\")</script></head></html>");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package controller.users;
|
||||
|
||||
import controller.roles.RolesControllerView;
|
||||
import model.User;
|
||||
|
||||
import javax.jdo.JDOObjectNotFoundException;
|
||||
@ -39,6 +40,7 @@ public class UsersControllerView extends HttpServlet {
|
||||
//Ya que se quiere editar, el atributo permitirEdicion es verdadero. Este atributo se comprueba en el JSP.
|
||||
request.setAttribute("editAllowed",true);
|
||||
request.setAttribute("action","Edit");
|
||||
request.setAttribute("Roles",RolesControllerView.getAllRoles());
|
||||
try{
|
||||
dispatcher.forward(request,response);
|
||||
} catch (javax.servlet.ServletException e){
|
||||
@ -50,6 +52,7 @@ public class UsersControllerView extends HttpServlet {
|
||||
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/View/Users/view.jsp");
|
||||
request.setAttribute("User",getUser(userID));
|
||||
request.setAttribute("UserLogged",getUser(request.getSession().getAttribute("userID").toString()));
|
||||
request.setAttribute("Roles",RolesControllerView.getAllRoles());
|
||||
|
||||
//Ya que no quiere editar, el atributo permitirEdicion es falso. Este atributo se comprueba en el JSP.
|
||||
request.setAttribute("editAllowed",false);
|
||||
|
@ -1,5 +1,7 @@
|
||||
package model;
|
||||
|
||||
import controller.roles.RolesControllerView;
|
||||
|
||||
import javax.jdo.annotations.IdentityType;
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.Persistent;
|
||||
@ -26,12 +28,12 @@ public class User {
|
||||
@Persistent
|
||||
private String email;
|
||||
|
||||
//Rol del Usuario
|
||||
//Rol del Usuario -> Lo que se alcamcena no es un objeto Role, sino la llave (key) de ese objeto.
|
||||
@Persistent
|
||||
private Role role;
|
||||
private String role;
|
||||
|
||||
//Constructor
|
||||
public User(String id, String name, String imgUrl, String email ,Role role){
|
||||
public User(String id, String name, String imgUrl, String email ,String role){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.imgUrl = imgUrl;
|
||||
@ -66,10 +68,13 @@ public class User {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Role getRole() {
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
public void setRole(Role role) {
|
||||
public String getRoleName() {
|
||||
return RolesControllerView.getRole(role).getName();
|
||||
}
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
|
@ -76,17 +76,17 @@
|
||||
</div>
|
||||
|
||||
<ul id="nav-mobile" class="right">
|
||||
<li class="active"><a class="whiteLink" href="">Users</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./roles')">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./resources')">Resources</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../users')">Users</a></li>
|
||||
<li class="active"><a class="whiteLink">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../resources')">Resources</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<br />
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif">Add a Role</span>
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif">Create a Role</span>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
|
@ -79,17 +79,17 @@
|
||||
</div>
|
||||
|
||||
<ul id="nav-mobile" class="right">
|
||||
<li><a class="whiteLink" onclick="postRedirect('./users')">Users</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../users')">Users</a></li>
|
||||
<li class="active"><a class="whiteLink" href="">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./resources')">Resources</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../resources')">Resources</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<br />
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif"><%=action%> a User</span>
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif"><%=action%> a Role</span>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
<%@ page import="model.User" %>
|
||||
<%@ page import="java.util.List" %><%--
|
||||
<%@ page import="java.util.List" %>
|
||||
<%@ page import="model.Role" %><%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: Fernando
|
||||
Date: 07/06/2018
|
||||
@ -8,6 +9,7 @@
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<% User user = (User) request.getAttribute("User"); %>
|
||||
<% List<Role> roles = (List<Role>) request.getAttribute("Roles"); %>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<title>Add a User - Hotel Services</title>
|
||||
@ -77,16 +79,16 @@
|
||||
|
||||
<ul id="nav-mobile" class="right">
|
||||
<li class="active"><a class="whiteLink" href="">Users</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./roles')">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./resources')">Resources</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../roles')">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../resources')">Resources</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<br />
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif">Add a User</span>
|
||||
<span style="font-size: xx-large; font-family: 'Product Sans',Roboto,serif">Create a User</span>
|
||||
<br />
|
||||
<br />
|
||||
|
||||
@ -110,7 +112,13 @@
|
||||
</div>
|
||||
|
||||
Role of the User:<br />
|
||||
<input name="userRole" placeholder="Role" required>
|
||||
<select name="userRole" class="browser-default" required>
|
||||
<option value="" disabled selected>Choose a Role</option>
|
||||
<% for(Role role: roles){ %>
|
||||
<option value="<%=role.getKey()%>"><%=role.getName()%></option>
|
||||
<% } %>
|
||||
</select>
|
||||
<br />
|
||||
|
||||
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
|
||||
<i class="material-icons right">send</i>
|
||||
|
@ -100,6 +100,7 @@
|
||||
<tr>
|
||||
<td>Name</td>
|
||||
<td>Email</td>
|
||||
<td>Role</td>
|
||||
<td>Img</td>
|
||||
<td>Actions</td>
|
||||
</tr>
|
||||
@ -112,6 +113,7 @@
|
||||
<tr>
|
||||
<td><%= user.getName()%></td>
|
||||
<td><%= user.getEmail()%></td>
|
||||
<td><%=user.getRoleName()%></td>
|
||||
<td><img src="<%= user.getImgUrl()%>" width="55px"/></td>
|
||||
<td>
|
||||
<a class="postLink" onclick="postRedirect('users/view',{action:'viewRedirect',userID:'<%=user.getId()%>'})">View</a>
|
||||
|
@ -1,5 +1,6 @@
|
||||
<%@ page import="model.User" %>
|
||||
<%@ page import="model.Role" %>
|
||||
<%@ page import="java.util.List" %>
|
||||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: Fernando
|
||||
@ -11,6 +12,7 @@
|
||||
User userLogged = (User) request.getAttribute("UserLogged");
|
||||
boolean editAllowed = (Boolean) request.getAttribute("editAllowed");
|
||||
String action = (String) request.getAttribute("action");%>
|
||||
<% List<Role> roles = (List<Role>) request.getAttribute("Roles"); %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
@ -80,9 +82,9 @@
|
||||
|
||||
<ul id="nav-mobile" class="right">
|
||||
<li class="active"><a class="whiteLink" href="">Users</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./roles')">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('./resources')">Resources</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../roles')">Roles</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../access')">Access</a></li>
|
||||
<li><a class="whiteLink" onclick="postRedirect('../resources')">Resources</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
@ -118,8 +120,13 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Rol<br />
|
||||
<input name="userRole" value="<%=user.getRole()%>" placeholder="Role" required><br />
|
||||
Role of the User:<br />
|
||||
<select name="userRole" class="browser-default" required>
|
||||
<option value="" disabled selected>Choose a Role</option>
|
||||
<% for(Role role: roles){ %>
|
||||
<option value="<%=role.getKey()%>"><%=role.getName()%></option>
|
||||
<% } %>
|
||||
</select>
|
||||
<br />
|
||||
|
||||
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
|
||||
@ -134,7 +141,7 @@
|
||||
<div class="col l8 m8" style="font-size: x-large">
|
||||
Name: <%=user.getName()%><br />
|
||||
Email: <%=user.getEmail()%><br />
|
||||
Role: <%= user.getRole()%><br />
|
||||
Role: <%= user.getRoleName()%><br />
|
||||
</div>
|
||||
<div class="col l4 m4">
|
||||
<img src="<%=user.getImgUrl()%>" width="96px"><br />
|
||||
|
Loading…
Reference in New Issue
Block a user