Skip to content

Commit

Permalink
[Delivers #80653262] Add admin search
Browse files Browse the repository at this point in the history
	Add an option in the webmaster cp for search admins using elasticsearch.
  • Loading branch information
LuuchoRocha committed Oct 30, 2014
1 parent 0eea252 commit db72246
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 107 deletions.
1 change: 0 additions & 1 deletion fastrun.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

export CLASPATH=""
for file in $(ls target/dependency); do export CLASSPATH=$CLASSPATH:target/dependency/$file; done
export CLASSPATH=$CLASSPATH:target/classes
Expand Down
16 changes: 16 additions & 0 deletions moustache/adminsearch.moustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form id="search_form">
<table border="0" class="reg_table">
<tr class="tr_header">
<td colspan="2">Buscar administradores</td>
</tr><tr>
<td>Ingrese su busqueda: </td>
<td>
<input type=text name="search_text">
</td>
</tr><tr class="tr_header">
<td colspan="2" align="center">
<input type=button value="Buscar" class="s_button" onclick="traer('administrators/search/response', 'columnacen', 'search_form')" />
</td>
</tr>
</table>
</form>
24 changes: 24 additions & 0 deletions moustache/adminsearch_response.moustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<table class="info_table">
<tr class="tr_header">
<td colspan="2">Resultados</td>
</tr>
<tr>
<td>ID</td>
<td>E-Mail</td>
</tr>

{{#administrators}}
<tr>
<td>
{{getId}}
</td>
<td>
{{toString}}
</td>
</tr>
{{/administrators}}

<tr class="tr_header">
<td colspan="2"></td>
</tr>
</table>
55 changes: 28 additions & 27 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ body {
color: #FFFFFF;
top: 0;
bottom: 0;
}
}

a {
color: white;
text-decoration: none;
}
}

a:hover {
color: white;
font-weight: bold;
text-decoration: none;
}
text-decoration: underline;
}

#header {
font-size:64px;
Expand All @@ -29,20 +29,30 @@ a:hover {
#cuerpo {
margin: 0 auto;
width: 80%;
}
}

#columnacen {
margin: 0 auto;
width: 70%;
width: 50%;
text-align: center;
background-color: rgba(0,0,0,0.4);
}
}

#columnacen table {
margin: 0 auto;
text-align: left;
}
}

#columnaizq {
float: left;
width: 15%;
}

#columnader {
float: right;
width: 15%;
}

.hidden_button{
position: absolute;
visibility: hidden;
Expand All @@ -54,13 +64,13 @@ a:hover {
border: 2px solid;
border-color: #403020;
border-collapse:collapse;
}
}

.info_table td,tr{
border: 2px solid;
border-color: #503010;
padding: 5px 10px;
}
}

.info_table .tr_header {
text-align: center;
Expand All @@ -74,38 +84,28 @@ a:hover {
border: 2px solid;
border-color: #503010;
border-collapse:collapse;
}
}

.reg_table .tr_header {
text-align: center;
font-weight: bold;
padding-bottom: 50px;
background-color: rgba(150,50,50, 0.1);
}

.reg_table td,tr{
border: 2px solid;
border-color: #503020;
padding: 5px 10px;
}


#columnaizq {
float: left;
width: 15%;
}

#columnader {
float: right;
width: 15%;
}

padding: 5px 10px;
}

.s_button {
color: #FFFFFF;
background-color: rgba(0,0,0, 0.3);
border: 2px;
border-style: solid;
border-color: #FFFFFF;
}
}

.popmenu {
text-align: center;
Expand All @@ -116,7 +116,7 @@ a:hover {
background-color: #0F0F0F;
background-color: rgba(255,255,255, 0.1);
width: 100%;
}
}

.popmenu:hover {
font-weight: bold;
Expand All @@ -135,6 +135,7 @@ a:hover {

.popmenu ul {
font-weight: normal;
color: yellow;
margin: 0;
padding: 0;
}
Expand Down
136 changes: 95 additions & 41 deletions src/main/java/com/unrc/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
import com.unrc.app.models.User;
import com.unrc.app.models.Vehicle;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.javalite.activejdbc.Base;
import spark.ModelAndView;
import spark.Request;
Expand Down Expand Up @@ -44,16 +49,9 @@ public static Session existsSession(Request request) {
*/
public static int sessionLevel(Session session){
if (null!=session) {
Boolean isWebmaster = session.attribute("email").toString().equals("admin");
Boolean isAdmin = Administrator.findByEmail(session.attribute("email")) != null;
Boolean isUser = User.findByEmail(session.attribute("email")) != null;
if (isWebmaster) return 3;
if (isAdmin) return 2;
if (isUser) return 1;
return 0;
} else {
return 0;
return session.attribute("level");
}
return 0;
}

/** Metodo para convertir una cadena a un entero con un radix 10
Expand All @@ -80,6 +78,7 @@ public static Integer strToInt(String s) {

public static void main(String[] args) {
externalStaticFileLocation("./public"); // Static files
ElasticSearch.client();

// <editor-fold desc="Spark filters (for db connection)">
Spark.before((request, response) -> {
Expand Down Expand Up @@ -241,7 +240,7 @@ public static void main(String[] args) {
);
//</editor-fold>

// <editor-fold desc="Sparks for user register">
// <editor-fold desc="Sparks for users">
get("/users/new",
(request, response) -> {
if (null == existsSession(request)) {
Expand Down Expand Up @@ -292,6 +291,35 @@ public static void main(String[] args) {
}
return body;
});

get("/users/del",
(request, response) -> {
if(sessionLevel(existsSession(request)) >= 2) {
Map<String, Object> attributes = new HashMap<>();

List<User> user = User.all();

attributes.put("users", user);

return new ModelAndView(attributes, "./moustache/userdel.moustache");
} else {
return new ModelAndView(null, "./moustache/notadmin.moustache");
}
},
new MustacheTemplateEngine()
);

delete("/users/:id", (request, response) -> {
String body = "";
User user = User.findById(request.params(":id"));
if(null != user){
user.delete();
body += "El usuario fue correctamente eliminado";
} else {
body += "El usuario no fue encontrado en la base de datos!";
}
return body;
});
//</editor-fold>

// <editor-fold desc="Sparks for city register">
Expand Down Expand Up @@ -558,6 +586,52 @@ public static void main(String[] args) {
new MustacheTemplateEngine()
);

get("/administrators/search",
(request, response) -> {
if(sessionLevel(existsSession(request)) >= 2) {
return new ModelAndView(null, "./moustache/adminsearch.moustache");
} else {
return new ModelAndView(null, "./moustache/notadmin.moustache");
}
},
new MustacheTemplateEngine()
);

get("/administrators/search/response",
(request, response) -> {
if(sessionLevel(existsSession(request)) >= 2) {
Map<String, Object> attributes = new HashMap<>();

Client client = ElasticSearch.client();

String search_text = request.queryParams("search_text");

SearchResponse searchResponse = client.prepareSearch("admins")
.setQuery(QueryBuilders.wildcardQuery("email", "*" + search_text + "*"))
.setSize(10)
.execute()
.actionGet();

List<Administrator> admins = new LinkedList<>();

searchResponse
.getHits()
.forEach(
(SearchHit h) -> {
admins.add(Administrator.findById(h.getId()));
}
);

attributes.put("administrators", admins);

return new ModelAndView(attributes, "./moustache/adminsearch_response.moustache");
} else {
return new ModelAndView(null, "./moustache/notadmin.moustache");
}
},
new MustacheTemplateEngine()
);

delete("/administrators/:id", (request, response) -> {
String body = "";
Administrator admin = Administrator.findById(request.params(":id"));
Expand Down Expand Up @@ -630,7 +704,8 @@ public static void main(String[] args) {
Session session = request.session(true);
session.attribute("email", email);
session.attribute("user_id", u.getId());
session.maxInactiveInterval(60);
session.attribute("level", 1);
session.maxInactiveInterval(120);
body += "<body><script type='text/javascript'>";
body += "alert('Bienvenido " + u + "!'); document.location = '/';";
body += "</script></body>";
Expand All @@ -640,12 +715,18 @@ public static void main(String[] args) {
if (a != null ? a.pass().equals(pwd) : false) {
Session session = request.session(true);
session.attribute("email", email);
session.maxInactiveInterval(60);

if (email.equals("admin"))
session.attribute("level", 3);
else
session.attribute("level", 2);

session.maxInactiveInterval(120);
body += "<body><script type='text/javascript'>";
body += "alert('Bienvenido administrador'); document.location = '/';";
body += "</script></body>";
return body;
}else{
} else {
body += "<body><script type='text/javascript'>";
body += "alert('El email indicado no existe o la contraseña no coincide.'); document.location = '/';";
body += "</script></body>";
Expand Down Expand Up @@ -698,34 +779,7 @@ public static void main(String[] args) {
});
//</editor-fold>

get("/users/del",
(request, response) -> {
if(sessionLevel(existsSession(request)) == 2) {
Map<String, Object> attributes = new HashMap<>();

List<User> user = User.all();

attributes.put("users", user);

return new ModelAndView(attributes, "./moustache/userdel.moustache");
} else {
return new ModelAndView(null, "./moustache/notuser.moustache");
}
},
new MustacheTemplateEngine()
);

delete("/users/:id", (request, response) -> {
String body = "";
User user = User.findById(request.params(":id"));
if(null != user){
user.delete();
body += "El usuario fue correctamente eliminado";
} else {
body += "El usuario no fue encontrado en la base de datos!";
}
return body;
});

}

}
Loading

0 comments on commit db72246

Please sign in to comment.