category.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * GET users listing.
  3. */
  4. import express = require('express');
  5. const router = express.Router();
  6. import Security from '../src/Security';
  7. import { ILDAPManager } from '../src/ldapInterface';
  8. import LDAPEntry from '../src/LDAPEntry';
  9. import ConfigManager, { LDAPAttribute, LDAPCategory } from '../src/ConfigLoader';
  10. function GetCurrentCategory(req: express.Request, defaultResult: LDAPCategory): LDAPCategory {
  11. if (!req.query["category"])
  12. return defaultResult;
  13. let query = Array.isArray(req.query["category"]) ? req.query["category"][0] : req.query["category"];
  14. return LinkToCategory(query.toString()) || defaultResult;
  15. }
  16. function LinkToCategory(query: string): LDAPCategory | null {
  17. return ConfigManager.GetInstance().GetCategoryByName(query);
  18. }
  19. function CategoryToLink(category: LDAPCategory): string {
  20. return category.GetName();
  21. }
  22. function DnToLink(dn: string): string {
  23. return dn.replace(new RegExp("=", "g"), "-").replace(new RegExp("[^a-zA-Z\-]", "g"), "_");
  24. }
  25. router.get('/', (req: express.Request, res: express.Response) => {
  26. if (!Security.requireLoggedUser(req, res))
  27. return;
  28. req.ldapManager.GetInstance().then((ldap: ILDAPManager): void => {
  29. let categories = ConfigManager.GetInstance().GetCategories();
  30. let category = GetCurrentCategory(req, categories[0]);
  31. ldap.ListEntries(category).then(items => {
  32. res.render("category", {
  33. categoryName: category.GetName(),
  34. allCategories: categories.map((i) => { return { name: i.GetName(), lnk: "?category="+CategoryToLink(i) }}),
  35. items: items,
  36. attributes: category.GetAttributes(),
  37. DnToLnk: DnToLink,
  38. GetAttributeLink: function (attr: LDAPAttribute, entityDn: string) {
  39. if (attr.type.startsWith("entry")) {
  40. let link = attr.type.split("#", 2)[1];
  41. if (link)
  42. return "?category=" + link + "#" + DnToLink(entityDn);
  43. }
  44. return null;
  45. },
  46. GetAttributeValue: function (user: LDAPEntry, attribute: LDAPAttribute) {
  47. return user.GetAttributeByName(attribute.name);
  48. }
  49. });
  50. });
  51. });
  52. });
  53. export default router;