From db72246d1ff0c3f43e983d7bc72897099b98ff40 Mon Sep 17 00:00:00 2001 From: Luciano Rocha Date: Thu, 30 Oct 2014 18:08:52 -0300 Subject: [PATCH] [Delivers #80653262] Add admin search Add an option in the webmaster cp for search admins using elasticsearch. --- fastrun.sh | 1 - moustache/adminsearch.moustache | 16 +++ moustache/adminsearch_response.moustache | 24 ++++ public/style.css | 55 +++---- src/main/java/com/unrc/app/App.java | 136 ++++++++++++------ src/main/java/com/unrc/app/App.java~ | 29 ---- src/main/java/com/unrc/app/ElasticSearch.java | 14 +- .../com/unrc/app/models/Administrator.java | 28 +++- 8 files changed, 196 insertions(+), 107 deletions(-) create mode 100644 moustache/adminsearch.moustache create mode 100644 moustache/adminsearch_response.moustache delete mode 100644 src/main/java/com/unrc/app/App.java~ diff --git a/fastrun.sh b/fastrun.sh index b6c75be..5670749 100755 --- a/fastrun.sh +++ b/fastrun.sh @@ -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 diff --git a/moustache/adminsearch.moustache b/moustache/adminsearch.moustache new file mode 100644 index 0000000..3c027c3 --- /dev/null +++ b/moustache/adminsearch.moustache @@ -0,0 +1,16 @@ +
+ + + + + + + + + +
Buscar administradores
Ingrese su busqueda: + +
+ +
+
diff --git a/moustache/adminsearch_response.moustache b/moustache/adminsearch_response.moustache new file mode 100644 index 0000000..bcef3a6 --- /dev/null +++ b/moustache/adminsearch_response.moustache @@ -0,0 +1,24 @@ + + + + + + + + + + {{#administrators}} + + + + + {{/administrators}} + + + + +
Resultados
IDE-Mail
+ {{getId}} + + {{toString}} +
diff --git a/public/style.css b/public/style.css index aa37718..34b23b8 100755 --- a/public/style.css +++ b/public/style.css @@ -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; @@ -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; @@ -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; @@ -74,7 +84,7 @@ a:hover { border: 2px solid; border-color: #503010; border-collapse:collapse; - } +} .reg_table .tr_header { text-align: center; @@ -82,30 +92,20 @@ a:hover { 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; @@ -116,7 +116,7 @@ a:hover { background-color: #0F0F0F; background-color: rgba(255,255,255, 0.1); width: 100%; - } +} .popmenu:hover { font-weight: bold; @@ -135,6 +135,7 @@ a:hover { .popmenu ul { font-weight: normal; + color: yellow; margin: 0; padding: 0; } diff --git a/src/main/java/com/unrc/app/App.java b/src/main/java/com/unrc/app/App.java index 6e4d1e3..62dc241 100644 --- a/src/main/java/com/unrc/app/App.java +++ b/src/main/java/com/unrc/app/App.java @@ -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; @@ -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 @@ -80,6 +78,7 @@ public static Integer strToInt(String s) { public static void main(String[] args) { externalStaticFileLocation("./public"); // Static files + ElasticSearch.client(); // Spark.before((request, response) -> { @@ -241,7 +240,7 @@ public static void main(String[] args) { ); // - // + // get("/users/new", (request, response) -> { if (null == existsSession(request)) { @@ -292,6 +291,35 @@ public static void main(String[] args) { } return body; }); + + get("/users/del", + (request, response) -> { + if(sessionLevel(existsSession(request)) >= 2) { + Map attributes = new HashMap<>(); + + List 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; + }); // // @@ -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 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 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")); @@ -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 += ""; @@ -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 += ""; return body; - }else{ + } else { body += ""; @@ -698,34 +779,7 @@ public static void main(String[] args) { }); // - get("/users/del", - (request, response) -> { - if(sessionLevel(existsSession(request)) == 2) { - Map attributes = new HashMap<>(); - - List 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; - }); + } } diff --git a/src/main/java/com/unrc/app/App.java~ b/src/main/java/com/unrc/app/App.java~ deleted file mode 100644 index 5b04243..0000000 --- a/src/main/java/com/unrc/app/App.java~ +++ /dev/null @@ -1,29 +0,0 @@ -package com.unrc.app; - -import org.javalite.activejdbc.Base; - -import com.unrc.app.models.User; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello cruel World!" ); - - Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", ""); - - User user = new User(); - user.set("first_name", "Marilyn"); - user.set("last_name", "Monroe"); - // user.set("dob", "1935-12-06"); - user.saveIt(); - - User.createIt("first_name", "Marcelo", "last_name", "Uva"); - - Base.close(); - } -} diff --git a/src/main/java/com/unrc/app/ElasticSearch.java b/src/main/java/com/unrc/app/ElasticSearch.java index 19f9c0e..83dba89 100644 --- a/src/main/java/com/unrc/app/ElasticSearch.java +++ b/src/main/java/com/unrc/app/ElasticSearch.java @@ -1,11 +1,11 @@ package com.unrc.app; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.client.Client; import org.elasticsearch.node.Node; -public class ElasticSearch { - // - public static final Node node = org.elasticsearch.node.NodeBuilder +public class ElasticSearch { + private static final Node node = org.elasticsearch.node.NodeBuilder .nodeBuilder() .clusterName("carsapp") .local(true) @@ -14,5 +14,11 @@ public class ElasticSearch { public static Client client(){ return node.client(); } - // + + public static void deleteAllIndexs(){ + client().admin().indices().delete(new DeleteIndexRequest("admins")).actionGet(); + client().admin().indices().delete(new DeleteIndexRequest("users")).actionGet(); + client().admin().indices().delete(new DeleteIndexRequest("posts")).actionGet(); + client().admin().indices().delete(new DeleteIndexRequest("vehicles")).actionGet(); + } } diff --git a/src/main/java/com/unrc/app/models/Administrator.java b/src/main/java/com/unrc/app/models/Administrator.java index e54fadb..6c6feae 100644 --- a/src/main/java/com/unrc/app/models/Administrator.java +++ b/src/main/java/com/unrc/app/models/Administrator.java @@ -22,13 +22,31 @@ public boolean createAdmin(String email, String pass){ @Override public void afterCreate(){ super.afterCreate(); + Map json = new HashMap<>(); - json.put("email", this.get("email")); + json.put("email", this.email()); - ElasticSearch.client().prepareIndex("admins", "admin", this.getId().toString()) - .setSource(json) - .execute() - .actionGet(); + ElasticSearch.client() + .prepareIndex() + .setIndex("admins") + .setType("admin") + .setId(this.getId().toString()) + .setSource(json) + .execute() + .actionGet(); + } + + @Override + public void beforeDelete(){ + super.beforeDelete(); + + ElasticSearch.client() + .prepareDelete() + .setIndex("admins") + .setType("admin") + .setId(this.getId().toString()) + .execute() + .actionGet(); } public static List all(){