From: Michael Tremer Date: Mon, 28 Oct 2019 19:35:16 +0000 (+0000) Subject: people: Show group memberships X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d8b04c72a513b55e926ecf1eb88334db831e5ebc;p=ipfire.org.git people: Show group memberships Signed-off-by: Michael Tremer --- diff --git a/src/backend/accounts.py b/src/backend/accounts.py index 3e6412f2..57e3b8c2 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -679,16 +679,14 @@ class Account(Object): @lazy_property def groups(self): groups = self.memcache.get("accounts:%s:groups" % self.dn) - if groups: - return groups + if not groups: + # Fetch groups from LDAP + groups = self._get_groups() - # Fetch groups from LDAP - groups = self._get_groups() + # Cache groups for 5 min + self.memcache.set("accounts:%s:groups" % self.dn, groups, 300) - # Cache groups for 5 min - self.memcache.set("accounts:%s:groups" % self.dn, groups, 300) - - return groups + return sorted((Group(self.backend, gid) for gid in groups)) def _get_groups(self): groups = [] @@ -1066,6 +1064,67 @@ class StopForumSpam(Object): return 100 - min(confidences) + +class Groups(Object): + @property + def search_base(self): + return "ou=Group,%s" % self.backend.accounts.search_base + + +class Group(Object): + def init(self, gid): + self.gid = gid + + def __repr__(self): + if self.description: + return "<%s %s (%s)>" % ( + self.__class__.__name__, + self.gid, + self.description, + ) + + return "<%s %s>" % (self.__class__.__name__, self.gid) + + def __str__(self): + return self.description or self.gid + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.gid == other.gid + + def __lt__(self, other): + if isinstance(other, self.__class__): + return (self.description or self.gid) < (other.description or other.gid) + + @lazy_property + def attributes(self): + attrs = self.memcache.get("groups:%s:attrs" % self.gid) + if not attrs: + attrs = self._get_attrs() + + # Cache this for 5 mins + self.memcache.set("groups:%s:attrs" % self.gid, attrs, 300) + + return attrs + + def _get_attrs(self): + res = self.backend.accounts._query( + "(&(|(objectClass=posixGroup)(objectClass=groupOfNames))(cn=%s))" % self.gid, + search_base=self.backend.groups.search_base, limit=1) + + for dn, attrs in res: + return attrs + + @property + def description(self): + try: + description = self.attributes["description"][0] + except KeyError: + return None + + return description.decode() + + if __name__ == "__main__": a = Accounts() diff --git a/src/backend/base.py b/src/backend/base.py index e0af11ca..ebd74a00 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -121,6 +121,10 @@ class Backend(object): if r: raise SystemExit(r) + @lazy_property + def groups(self): + return accounts.Groups(self) + @lazy_property def messages(self): return messages.Messages(self) diff --git a/src/templates/people/user.html b/src/templates/people/user.html index edbef8ae..f3736729 100644 --- a/src/templates/people/user.html +++ b/src/templates/people/user.html @@ -3,7 +3,7 @@ {% block main %} {% import phonenumbers %} -
+
@@ -78,6 +78,25 @@
{% end %}
+ + {% if account.groups %} +
+
+
+
{{ _("Groups") }}
+
+
+
+ +
    + {% for g in account.groups %} +
  • + + {{ g }} +
  • + {% end %} +
+ {% end %}