Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created November 20, 2013 15:17
Show Gist options
  • Save cfalzone/7564850 to your computer and use it in GitHub Desktop.
Save cfalzone/7564850 to your computer and use it in GitHub Desktop.
Gets user by role
<%@page import="com.dotmarketing.business.RoleFactory"%>
<%@page import="com.dotmarketing.business.RoleFactoryImpl"%>
<%@page import="com.dotmarketing.business.DotCacheAdministrator"%>
<%@page import="com.dotmarketing.business.RoleCacheImpl"%>
<%@page import="com.dotmarketing.business.RoleCache"%>
<%@page import="com.dotmarketing.business.Role"%>
<%@page import="com.dotmarketing.util.UtilMethods"%>
<%@page import="com.ibm.icu.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Map"%>
<%@page import="com.dotmarketing.common.db.DotConnect"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Role Report</title>
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/cupertino/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".roles").accordion({heightStyle: "content", collapsible:true, active:false});
$(".roles h3").css('padding-left', '20px');
});
</script>
</head>
<body>
<h1> Users in Roles</h1>
<div class="wrapper">
<div class="roles">
<%
String rolesql = "select id, parent, role_name from cms_role where system=0 order by role_name";
DotConnect dc = new DotConnect();
dc.setSQL(rolesql);
List roles = dc.loadResults();
for(int i=0; i<roles.size(); i++){
Map role = (Map) roles.get(i);
String parent = "ROOT";
if(UtilMethods.isSet(role.get("parent")) && !((String)role.get("parent")).equals((String)role.get("id"))) {
String parentsql = "select role_name from cms_role where id='"+role.get("parent")+"'";
dc = new DotConnect();
dc.setSQL(parentsql);
List parents = dc.loadResults();
if(parents.size() > 0) parent = (String) ((Map)parents.get(0)).get("role_name");
}
String userssql = "select user_id from users_cms_roles where role_id='"+role.get("id")+"'";
dc = new DotConnect();
dc.setSQL(userssql);
List users = dc.loadResults();
%>
<h2><%=parent%> &raquo; <%=role.get("role_name")%> (<%=users.size()%>)</h2>
<div class="role">
<div class="users">
<table class="user"><thead><tr><th>Name</th><th>Email</th><th>Last Login</th></tr></thead><tbody>
<%
for(int j=0; j<users.size(); j++){
String userid = (String) ((Map) users.get(j)).get("user_id");
String usersql = "select userId, firstName, lastName, emailAddress, lastLoginDate from user_ where userId='"+userid+"'";
dc = new DotConnect();
dc.setSQL(usersql);
List l = dc.loadResults();
Map user = new HashMap();
if(l.size() > 0) {
user = (Map) l.get(0);
} else {
continue;
}
String lastLoginDB = (String) user.get("lastlogindate");
String lastLoginStr = "Never";
if(UtilMethods.isSet(lastLoginDB)) {
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
Date lastLoginDate = formatter.parse(lastLoginDB);
lastLoginStr = formatter.format(lastLoginDate);
}
%>
<tr><td><%=user.get("firstname")%> <%=user.get("lastname")%></td><td><%=user.get("emailaddress")%></td><td><%=lastLoginStr%></td></tr>
<%
}
%>
</tbody></table>
</div>
</div>
<%
}
%>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment