PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/myproject/src/com/bp/pensionline/reporting/util/ContentPermissionReportUtil.java

https://bitbucket.org/tuannguyen/mytest
Java | 278 lines | 219 code | 30 blank | 29 comment | 31 complexity | b06613f225965ca6f6532c850ea10d2c MD5 | raw file
  1. /**
  2. * Utility class for retrieving CMS resources and group permission
  3. */
  4. package com.bp.pensionline.reporting.util;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFFont;
  13. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import org.apache.poi.hssf.util.HSSFColor;
  18. import org.opencms.file.CmsFile;
  19. import org.opencms.file.CmsFolder;
  20. import org.opencms.file.CmsObject;
  21. import org.opencms.file.types.CmsResourceTypeJsp;
  22. import org.opencms.file.types.CmsResourceTypeXmlContent;
  23. import org.opencms.main.CmsException;
  24. import org.opencms.main.CmsLog;
  25. import org.opencms.main.OpenCms;
  26. import org.opencms.security.CmsAccessControlEntry;
  27. import org.opencms.security.CmsPermissionSet;
  28. import org.opencms.security.CmsPrincipal;
  29. import org.opencms.security.I_CmsPrincipal;
  30. import com.bp.pensionline.util.SystemAccount;
  31. /**
  32. * @author AS5920G
  33. *
  34. */
  35. public class ContentPermissionReportUtil {
  36. public static final Log LOG = CmsLog.getLog(ContentPermissionReportUtil.class);
  37. public static String buildTable(HttpServletRequest request) {
  38. StringBuffer buf = new StringBuffer();
  39. buf.append("<table width='100%' cellspacing='0' cellpadding='0'>");
  40. buf.append("<thead>");
  41. buf.append(" <tr height='20px'>");
  42. buf.append(" <td width='30%' style='background-color:#037e01; color:#FFFFFF; font-weight: bold'>&nbsp;&nbsp;Resource</td>");
  43. buf.append(" <td width='70%' style='background-color:#037e01; color:#FFFFFF; font-weight: bold'>Restriction</td>");
  44. buf.append(" </tr>");
  45. buf.append("</thead>");
  46. buf.append("<tbody>");
  47. buf.append(" <tr height='20px'>");
  48. buf.append(" <td colspan='2' style='padding: 0px;'>");
  49. buf.append(" <div style='width:100%; height:600px; overflow-x: hidden; overflow-y: scroll;'>");
  50. buf.append(" <table class='publishtable' cellpadding='0' cellspacing='1' style='width: 100%; table-layout: fixed;'>");
  51. List<String> l = getResourceAndPermission();
  52. int idx = -1;
  53. for (int i=0; i<l.size(); i++) {
  54. String s = l.get(i);
  55. String[] p = s.split("@");
  56. idx = p[0].indexOf("/sites/default");
  57. if (idx != -1){
  58. p[0] = p[0].substring(14);
  59. }
  60. buf.append(" <tr>");
  61. buf.append(" <td width='30%' style='word-wrap: break-word; break-word: break-all;'>");
  62. buf.append(" <a href='/content/pl"+p[0]+"'>"+p[0]);
  63. buf.append(" </td>");
  64. buf.append(" <td width='70%' style='word-wrap: break-word; break-word: break-all;'>");
  65. buf.append( p[1]);
  66. buf.append(" </td>");
  67. buf.append(" </tr>");
  68. }
  69. buf.append(" </table>");
  70. buf.append(" </td>");
  71. buf.append(" </tr>");
  72. buf.append("</tbody>");
  73. buf.append("</table>");
  74. request.getSession().setAttribute("xlsContent", l);
  75. // try {
  76. // HSSFWorkbook doc = createXLSSource(l);
  77. // byte[] b = doc.getBytes();
  78. // //request.getSession().removeAttribute("xlsContent");
  79. // request.getSession().setAttribute("xlsContent", doc);
  80. // FileOutputStream stream = new FileOutputStream("D:/resource_restriction.xls");
  81. // doc.write(stream);
  82. // stream.close();
  83. //
  84. // } catch (Exception e) {
  85. // e.printStackTrace();
  86. // }
  87. return buf.toString();
  88. }
  89. //create excel document
  90. public static HSSFWorkbook createXLSSource(List<String> l) {
  91. LOG.info("createXLSSource():BEGIN");
  92. HSSFWorkbook wb = new HSSFWorkbook();
  93. HSSFSheet sheet = wb.createSheet("Resources Restriction");
  94. HSSFCellStyle headingStyle = wb.createCellStyle();
  95. HSSFFont headingFont = wb.createFont();
  96. headingFont.setFontHeightInPoints((short)10);
  97. headingFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  98. headingFont.setColor(HSSFColor.WHITE.index);
  99. headingStyle.setFont(headingFont);
  100. headingStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  101. headingStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  102. headingStyle.setFillForegroundColor(HSSFColor.GREEN.index);
  103. headingStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  104. HSSFCellStyle rowStyle = wb.createCellStyle();
  105. rowStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  106. rowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  107. rowStyle.setWrapText(true);
  108. HSSFRow horzTitle = sheet.createRow(0);
  109. HSSFCell source = horzTitle.createCell(0);
  110. source.setCellStyle(headingStyle);
  111. source.setCellValue(new HSSFRichTextString("Resource"));
  112. HSSFCell restriction = horzTitle.createCell(1);
  113. restriction.setCellStyle(headingStyle);
  114. restriction.setCellValue(new HSSFRichTextString("Restriction"));
  115. sheet.setColumnWidth(0, 8000);
  116. sheet.setColumnWidth(1, 10000);
  117. int idx = -1;
  118. LOG.info("Records: "+l.size());
  119. for (int i=0; i<l.size(); i++) {
  120. String s = l.get(i);
  121. String[] p = s.split("@");
  122. idx = p[0].indexOf("/sites/default");
  123. if (idx != -1) p[0] = p[0].substring(14);
  124. HSSFRow row = sheet.createRow(i+1);
  125. HSSFCell c1 = row.createCell(0);
  126. c1.setCellStyle(rowStyle);
  127. c1.setCellValue(new HSSFRichTextString(p[0]));
  128. HSSFCell c2 = row.createCell(1);
  129. c2.setCellStyle(rowStyle);
  130. c2.setCellValue(new HSSFRichTextString(p[1]));
  131. }
  132. LOG.info("createXLSSource():END");
  133. return wb;
  134. }
  135. private static List<String> getResourceAndPermission() {
  136. List<String> list = new ArrayList<String>();
  137. CmsObject admin = SystemAccount.getPublishingAdminCmsObject();
  138. try {
  139. List files = admin.getFilesInFolder("/sites/default/");
  140. List folders = admin.getSubFolders("/sites/default/");
  141. List groups = OpenCms.getOrgUnitManager().getGroups(admin, "/", false);
  142. for (int i=0; i<files.size(); i++) {
  143. CmsFile file = (CmsFile)files.get(i);
  144. if (file.getTypeId() != CmsResourceTypeJsp.getStaticTypeId()) {
  145. String p = file.getRootPath(); //string start with source path
  146. //retrieve group permission on resource
  147. String restriction = "";
  148. Iterator itAces = admin.getAccessControlEntries(p, false).iterator();
  149. while (itAces.hasNext()) {
  150. CmsAccessControlEntry curEntry = (CmsAccessControlEntry)itAces.next();
  151. I_CmsPrincipal principal = CmsPrincipal.readPrincipalIncludingHistory(admin,
  152. curEntry.getPrincipal());
  153. if (!curEntry.isInherited()) {
  154. // check if the entry have -r, -v
  155. CmsPermissionSet pm = curEntry.getPermissions();
  156. if (!(pm.requiresReadPermission() && pm.requiresViewPermission())) {
  157. restriction += "," + principal.getName();
  158. }
  159. }
  160. }
  161. if (restriction.indexOf(',')!=-1) {
  162. p += "@"+restriction.substring(1);
  163. } else {
  164. p += "@No restrictions ";
  165. }
  166. list.add(p);
  167. }
  168. }
  169. //continue with sub folders
  170. for (int i=0; i<folders.size(); i++) {
  171. list.addAll(recursive(admin, ((CmsFolder)folders.get(i)).getRootPath()));
  172. }
  173. //continue with galleries /system/galleries/
  174. list.addAll(recursive(admin, "/system/galleries/"));
  175. } catch (CmsException cme) {
  176. LOG.error("Exception "+cme.toString());
  177. } catch (Exception e) {
  178. LOG.error("Exception "+e.toString());
  179. e.printStackTrace();
  180. }
  181. return list;
  182. }
  183. private static List<String> recursive(CmsObject admin, String path) {
  184. List<String> list = new ArrayList<String>();
  185. try {
  186. String r = "";
  187. Iterator iter = admin.getAccessControlEntries(path, false).iterator();
  188. while (iter.hasNext()) {
  189. CmsAccessControlEntry curEntry = (CmsAccessControlEntry)iter.next();
  190. I_CmsPrincipal principal = CmsPrincipal.readPrincipalIncludingHistory(admin,
  191. curEntry.getPrincipal());
  192. if (!curEntry.isInherited()) {
  193. // check if the entry have -r, -v
  194. CmsPermissionSet pm = curEntry.getPermissions();
  195. if (!(pm.requiresReadPermission() && pm.requiresViewPermission())) {
  196. r += "," + principal.getName();
  197. }
  198. }
  199. }
  200. if (r.indexOf(',')!=-1) {
  201. r = r.substring(1);
  202. } else {
  203. r ="No specifically assigned restrictions, but they do inherit from the parent";
  204. }
  205. list.add(path+"@"+r);
  206. List files = admin.getFilesInFolder(path);
  207. List folders = admin.getSubFolders(path);
  208. //work with files in folder
  209. for (int i=0; i<files.size(); i++) {
  210. CmsFile file = (CmsFile)files.get(i);
  211. if (!CmsResourceTypeXmlContent.isXmlContent(file)) {
  212. String p = file.getRootPath(); //string start with source path
  213. //retrieve group permission on resource
  214. String restriction = "";
  215. Iterator itAces = admin.getAccessControlEntries(p, false).iterator();
  216. while (itAces.hasNext()) {
  217. CmsAccessControlEntry curEntry = (CmsAccessControlEntry)itAces.next();
  218. I_CmsPrincipal principal = CmsPrincipal.readPrincipalIncludingHistory(admin,
  219. curEntry.getPrincipal());
  220. if (!curEntry.isInherited()) {
  221. // check if the entry have -r, -v
  222. CmsPermissionSet pm = curEntry.getPermissions();
  223. if (!(pm.requiresReadPermission() && pm.requiresViewPermission())) {
  224. restriction += "," + principal.getName();
  225. }
  226. }
  227. }
  228. if (restriction.indexOf(',')!=-1) {
  229. p += "@"+restriction.substring(1);
  230. } else {
  231. p += "@No specifically assigned restrictions, but they do inherit from the parent";
  232. }
  233. list.add(p);
  234. }
  235. }
  236. //continue with sub folders
  237. for (int i=0; i<folders.size(); i++) {
  238. list.addAll(recursive(admin, ((CmsFolder)folders.get(i)).getRootPath()));
  239. }
  240. } catch (CmsException cme) {
  241. LOG.error("Exception "+cme.toString());
  242. } catch (Exception e) {
  243. LOG.error("Exception "+e.toString());
  244. e.printStackTrace();
  245. }
  246. return list;
  247. }
  248. }