From: Michael Tremer Date: Thu, 15 Aug 2019 13:36:01 +0000 (+0100) Subject: people: Show list of recently created user accounts X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9150881ec3583efd76e1da6cb50880d99f8616b7;p=ipfire.org.git people: Show list of recently created user accounts Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 12cf7e16..6518dd83 100644 --- a/Makefile.am +++ b/Makefile.am @@ -238,6 +238,7 @@ templates_peopledir = $(templatesdir)/people templates_people_modules_DATA = \ src/templates/people/modules/accounts-list.html \ + src/templates/people/modules/accounts-new.html \ src/templates/people/modules/cdr.html \ src/templates/people/modules/channels.html \ src/templates/people/modules/mos.html \ diff --git a/src/backend/accounts.py b/src/backend/accounts.py index d68e2739..1b59882c 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -54,7 +54,8 @@ class Accounts(Object): logging.info("Successfully authenticated as %s" % self.ldap.whoami_s()) def _query(self, query, attrlist=None, limit=0, search_base=None): - logging.debug("Performing LDAP query: %s" % query) + logging.debug("Performing LDAP query (%s): %s" \ + % (search_base or self.search_base, query)) t = time.time() @@ -78,7 +79,8 @@ class Accounts(Object): """ Fetches all attributes for the given distinguished name """ - results = self._query("(objectClass=*)", search_base=dn, limit=1) + results = self._query("(objectClass=*)", search_base=dn, limit=1, + attrlist=("*", "createTimestamp", "modifyTimestamp")) for dn, attrs in results: return attrs @@ -94,6 +96,11 @@ class Accounts(Object): return Account(self.backend, dn, attrs) + def get_created_after(self, ts): + t = ts.strftime("%Y%m%d%H%M%SZ") + + return self._search("(&(objectClass=person)(createTimestamp>=%s))" % t) + def search(self, query): # Search for exact matches accounts = self._search( @@ -322,6 +329,12 @@ class Account(Object): for value in self._get_strings(key): yield phonenumbers.parse(value, None) + def _get_timestamp(self, key): + value = self._get_string(key) + + # Parse the timestamp value and returns a datetime object + return datetime.datetime.strptime(value, "%Y%m%d%H%M%SZ") + def _modify(self, modlist): logging.debug("Modifying %s: %s" % (self.dn, modlist)) @@ -540,6 +553,16 @@ class Account(Object): return groups + # Created/Modified at + + @property + def created_at(self): + return self._get_timestamp("createTimestamp") + + @property + def modified_at(self): + return self._get_timestamp("modifyTimestamp") + # Address @property diff --git a/src/templates/people/modules/accounts-new.html b/src/templates/people/modules/accounts-new.html new file mode 100644 index 00000000..5898cb72 --- /dev/null +++ b/src/templates/people/modules/accounts-new.html @@ -0,0 +1,5 @@ +{% if accounts %} +

{{ _("Recently Created Accounts") }}

+ + {% module AccountsList(accounts) %} +{% end %} diff --git a/src/templates/people/users.html b/src/templates/people/users.html index 512c8201..f8841029 100644 --- a/src/templates/people/users.html +++ b/src/templates/people/users.html @@ -7,7 +7,7 @@

{{ _("Users") }}

- {% module AccountsList() %} + {% module NewAccounts() %}
{% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index af11c98f..2a1b96f0 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -77,6 +77,7 @@ class Application(tornado.web.Application): "CDR" : people.CDRModule, "Channels" : people.ChannelsModule, "MOS" : people.MOSModule, + "NewAccounts" : people.NewAccountsModule, "Password" : people.PasswordModule, "Registrations" : people.RegistrationsModule, "SIPStatus" : people.SIPStatusModule, diff --git a/src/web/people.py b/src/web/people.py index 581ba120..cfbeef01 100644 --- a/src/web/people.py +++ b/src/web/people.py @@ -358,6 +358,19 @@ class UserPasswdHandler(auth.CacheMixin, base.BaseHandler): self.redirect("/users/%s" % account.uid) +class NewAccountsModule(ui_modules.UIModule): + def render(self, days=14): + t = datetime.datetime.utcnow() - datetime.timedelta(days=days) + + # Fetch all accounts created after t + accounts = self.backend.accounts.get_created_after(t) + + accounts.sort(key=lambda a: a.created_at, reverse=True) + + return self.render_string("people/modules/accounts-new.html", + accounts=accounts, t=t) + + class AccountsListModule(ui_modules.UIModule): def render(self, accounts=None): if accounts is None: