diff --git a/app.py b/app.py index 736ba42..80bcc63 100644 --- a/app.py +++ b/app.py @@ -86,6 +86,13 @@ def get_index(): except Exception as e: return index_tpl(str=i18n.str) +@get('/logs') +def get_index(): + try: + return logs_tpl(data=newSession().get(), str=i18n.str) + except Exception as e: + return index_tpl(str=i18n.str) + @get('/delete') def get_index(): try: @@ -93,6 +100,7 @@ def get_index(): except Exception as e: return index_tpl(str=i18n.str) + @get('/logout') def get_index(): @@ -342,6 +350,9 @@ def edit_fullname_tpl(**kwargs): def delete_tpl(**kwargs): return template('delete', **kwargs) +def logs_tpl(**kwargs): + return template('logs', **kwargs) + def connect_ldap(conf, **kwargs): server = Server(host=conf['host'], port=conf.getint('port', None), @@ -398,9 +409,11 @@ def login_user_ldap(conf, username, password): # Note: raises LDAPUserNameIsMandatoryError when user_dn is None. with connect_ldap(conf, authentication=SIMPLE, user=user_dn, password=password) as c: c.bind() - update_login_info(conf, user_dn) - newSession().set(get_user_data(user_dn, c)) - LOG.debug("%s logged in to %s" % (username, conf['base'])) + if is_trusted_device(conf, user_dn): + newSession().set(get_user_data(user_dn, c)) + #update timestamp + ip address + update_login_info(conf, user_dn) + LOG.debug("%s logged in to %s" % (username, conf['base'])) #LOGOUT def logout(username): @@ -804,7 +817,7 @@ def get_user_email_array(user_dn, conn, old_email, new_email): def get_user_data(user_dn, conn): search_filter = '(objectClass=*)' - conn.search(user_dn, search_filter, attributes=['active','fakeCn','givenName','sn','uid','mail','devices']) + conn.search(user_dn, search_filter, attributes=['active','fakeCn','givenName','sn','uid','mail','devices','ip','lastLogin']) data = [] data.append(conn.entries[0].active.values[0]) data.append(conn.entries[0].fakeCn.values[0]) @@ -813,6 +826,12 @@ def get_user_data(user_dn, conn): data.append(conn.entries[0].uid.values[0]) data.append(conn.entries[0].mail.values[0]) data.append(conn.entries[0].devices.values) + data.append(conn.entries[0].ip.values[0]) + #ts = conn.entries[0].lastLogin.values[0] + #ts = datetime.strptime(ts, '%Y-%m-%d %H:%M:%S%z') + #ts = datetime.strftime(t, '%Y-%m-%d %H:%M:%S') + data.append(str(conn.entries[0].lastLogin.values[0])[:-6]) + return(data) def read_config(): @@ -843,16 +862,26 @@ def get_dev(): user_agent = parse(ua_string) return str(user_agent) +def is_trusted_device(conf, user_dn): + superUser = SuperUsers(conf) + with connect_ldap(conf, user=superUser.admin_dn, password=superUser.admin_pwd) as c: + d = get_dev() + if not find_device(conf, c, d): + OBJECT_CLASS = ['top', 'inetOrgPerson', 'posixAccount', 'accountsManagement'] + c.modify(user_dn, {'devices': [( MODIFY_ADD, d )] }) + c.unbind() + return True + def update_login_info(conf, user_dn): superUser = SuperUsers(conf) with connect_ldap(conf, user=superUser.admin_dn, password=superUser.admin_pwd) as c: ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr) t = datetime.now().strftime('%Y%m%d%H%M%S')+'Z' c.modify(user_dn, {'ip': [( MODIFY_REPLACE, str(ip) )], 'lastLogin': [( MODIFY_REPLACE, t )] }) - d = get_dev() - if not find_device(conf, c, d): - OBJECT_CLASS = ['top', 'inetOrgPerson', 'posixAccount', 'accountsManagement'] - c.modify(user_dn, {'devices': [( MODIFY_ADD, d )] }) + #d = get_dev() + #if not find_device(conf, c, d): + #OBJECT_CLASS = ['top', 'inetOrgPerson', 'posixAccount', 'accountsManagement'] + #c.modify(user_dn, {'devices': [( MODIFY_ADD, d )] }) c.unbind() class Error(Exception): @@ -893,6 +922,8 @@ def newSession(): self.username = data[4] self.mail = data[5] self.devices = data[6] + self.ip = data[7] + self.lastLogin = data[8] self.data['active'] = self.active self.data['fakeCn'] = self.fakeCn @@ -901,6 +932,8 @@ def newSession(): self.data['username'] = self.username self.data['mail'] = self.mail self.data['devices'] = self.devices + self.data['ip'] = self.ip + self.data['lastLogin'] = self.lastLogin def close(self): self.data.pop('username') diff --git a/logs.tpl b/logs.tpl new file mode 100644 index 0000000..0cd9129 --- /dev/null +++ b/logs.tpl @@ -0,0 +1,51 @@ + + + + + + + + + {{ str['edit-email'] }} + + + + + +
+

{{ str['edit-email'] }}Erregistroak

+ +
+ + + + +
+ + +
+ +
+
+ +
+
Devices
+
  • PC / Linux / Firefox 102.0
  • +
  • Samsung A5 2016 / Android 10 / Chrome
  • +
  • Iphone 5S / Iphone 11 / Safari 12
  • +
    +
    + + %for type, text, animation in get('alerts', []): +
    +
    {{ text }}
    +
    + %end + +
    + + diff --git a/static/style.css b/static/style.css index 36f0d92..e94b364 100644 --- a/static/style.css +++ b/static/style.css @@ -35,7 +35,7 @@ form[name="fullNameForm"] input{ } -form { +form, .logs { /* border-radius: 0.2rem; border: 1px solid #CCC;*/ margin: 0 auto; diff --git a/user.tpl b/user.tpl index f9d2a0e..105f7af 100644 --- a/user.tpl +++ b/user.tpl @@ -58,7 +58,7 @@
    - ikusi + ikusi