/* * GET users listing. */ import express = require('express'); const router = express.Router(); import Security from '../src/Security'; import { ILDAPManager } from '../src/ldapInterface'; import LDAPEntry from '../src/LDAPEntry'; import RouterUtils from '../src/RouterUtils'; import ConfigManager, { LDAPAttribute, LDAPCategory } from '../src/ConfigLoader'; function GetCurrentCategory(req: express.Request, defaultResult: LDAPCategory): LDAPCategory|null { if (!req.query["category"]) return null; let query = Array.isArray(req.query["category"]) ? req.query["category"][0] : req.query["category"]; return LinkToCategory(query.toString()) || defaultResult; } function LinkToCategory(query: string): LDAPCategory | null { return ConfigManager.GetInstance().GetCategoryByName(query); } function CategoryToLink(category: LDAPCategory): string { return category.GetName(); } function DnToLink(dn: string): string { return dn.replace(new RegExp("=", "g"), "-").replace(new RegExp("[^a-zA-Z\-]", "g"), "_"); } router.get('/', (req: express.Request, res: express.Response) => { if (!req.isUserLogged) return RouterUtils.Redirect(res, "/login"); req.ldapManager.GetInstance().then((ldap: ILDAPManager): void => { ldap.GetTree().then(root => { let categories = ConfigManager.GetInstance().GetCategories(); let category = GetCurrentCategory(req, categories[0]); if (category == null) { res.render("index", { tree: root.GetChildren()[0], rootSrc: "?category=" + CategoryToLink(categories[0]) }); } else { let session = Security.GetSession(req); ldap.ListEntries(category).then(items => { res.render("category", { csrf: session ? session.GetCSRFToken() : "", categoryName: category?.GetName(), allCategories: categories.map((i) => { return { name: i.GetName(), lnk: "?category=" + CategoryToLink(i) } }), items: items, attributes: category?.GetAttributes(), DnToLnk: DnToLink, GetAttributeLink: function (attr: LDAPAttribute, entityDn: string) { if (attr.type.startsWith("entry")) { let link = attr.type.split("#", 2)[1]; if (link) return "?category=" + link + "#" + DnToLink(entityDn); } return null; }, GetAttributeValue: function (user: LDAPEntry, attribute: LDAPAttribute) { return user.GetAttributeByName(attribute.name); } }); }); } }); }); }); export default router;