]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/accounts.py
people: Show group memberships
[ipfire.org.git] / src / backend / accounts.py
index 3e6412f26a4d40c7eed6ccf803f391f1781d5716..57e3b8c272750aeb0190770b89ca2c294257af92 100644 (file)
@@ -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()