Skip to content

Commit

Permalink
In the admin show information about username and last logon.
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Dec 24, 2021
1 parent 55d4749 commit 90db6f2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@

<div class="form-group">
<div class="alert alert-info">
{% if m.identity[id].is_user %}
{_ This person is also a user. _}
{% else %}
{_ This person is not yet a user. _}
{% endif %}
{% with m.identity[id].user_info as user_info %}
{% if user_info.username %}
{_ Username _} <b>{{ user_info.username|escape }}</b>,
{_ last logon at _} {{ user_info.visited|date:_"Y-m-d H:i" }}.
{% elseif m.identity[id].is_user %}
{_ This person is also a user. _}
{% else %}
{_ This person is not yet a user. _}
{% endif %}
{% endwith %}
</div>
</div>
{% endblock %}
14 changes: 10 additions & 4 deletions modules/mod_admin_identity/templates/admin_users.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,26 @@
<tr>
<th width="20%">{_ Name _}</th>
<th width="20%">{_ Username _}</th>
<th width="15%">{_ Last logon _}</th>
<th width="15%">{_ Created on _}</th>
<th width="40%">{_ Modified on _}</th>
<th width="25%">{_ Modified on _}</th>
</tr>
</thead>

<tbody>
{% for id in result %}
{% with m.identity[id].user_info as user_info %}
<tr id="{{ #tr.id }}" data-href="javascript:;" {% if not id.is_published %}class="unpublished"{% endif %}>
<td>{{ m.rsc[id].title|striptags }}</td>
<td>
{% if not m.identity[id].username %}
{% if not user_info.username %}
&mdash;
{% else %}
{{ m.identity[id].username|escape }}{% if id == me %} <strong>{_ (that's you) _}</strong>{% endif %}</td>
{% endif %}
{{ user_info.username|escape }}
{% if id == me %} <strong>{_ (that's you) _}</strong>{% endif %}
{% endif %}
</td>
<td>{{ user_info.visited|date:_"d M Y, H:i" }}</td>
<td>{{ id.created|date:_"d M Y, H:i" }}</td>
<td>
{{ id.modified|date:_"d M Y, H:i" }}
Expand All @@ -76,6 +81,7 @@
</td>
</tr>
{% wire id=" #"|append:#tr.id|append:" td" action={dialog_edit_basics id=id target=undefined} %}
{% endwith %}
{% empty %}
<tr>
<td colspan="4">
Expand Down
54 changes: 52 additions & 2 deletions src/models/m_identity.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
is_user/2,
get_username/1,
get_username/2,
get_user_info/1,
get_user_info/2,
delete_username/2,
set_username/3,
set_username_pw/4,
Expand Down Expand Up @@ -77,6 +79,8 @@
set_verified/4,
is_verified/2,

set_visited/2,

delete/2,
merge/3,
is_reserved_name/1,
Expand Down Expand Up @@ -106,6 +110,8 @@ m_find_value(is_user, #m{value=RscId}, Context) ->
is_user(RscId, Context);
m_find_value(username, #m{value=RscId}, Context) ->
get_username(RscId, Context);
m_find_value(user_info, #m{value=RscId}, Context) ->
get_user_info(RscId, Context);
m_find_value(all, #m{value=RscId} = M, _Context) ->
M#m{value={all, RscId}};
m_find_value(all_types, #m{value=RscId}, Context) ->
Expand Down Expand Up @@ -157,6 +163,51 @@ get_username(Id, Context) ->
z_db:q1("select key from identity where rsc_id = $1 and type = 'username_pw'", [m_rsc:rid(Id, Context)], Context).


%% @doc Return the username and last login of the current user.
-spec get_user_info(z:context()) -> map().
get_user_info(Context) ->
case z_acl:user(Context) of
undefined ->
#{
<<"user_id">> => undefined,
<<"username">> => undefined,
<<"visited">> => undefined,
<<"modified">> => undefined,
<<"is_expired">> => false
};
UserId ->
get_user_info(UserId, Context)
end.

%% @doc Return the username and last login of the resource id, undefined if no username
-spec get_user_info(m_rsc:resource_id(), z:context()) -> binary().
get_user_info(RscId, Context) when is_integer(RscId) ->
Row = z_db:q_row("
select key, visited, prop1, modified
from identity
where rsc_id = $1
and type = 'username_pw'",
[m_rsc:rid(RscId, Context)],
Context),
case Row of
undefined ->
#{
<<"user_id">> => RscId,
<<"username">> => undefined,
<<"visited">> => undefined,
<<"modified">> => undefined,
<<"is_expired">> => false
};
{Key, Visited, Prop1, Modified} ->
#{
<<"user_id">> => RscId,
<<"username">> => Key,
<<"visited">> => Visited,
<<"modified">> => Modified,
<<"is_expired">> => Prop1 =:= <<"expired">>
}
end.

%% @doc Check if the user is allowed to change the username of a resource.
-spec is_allowed_set_username( m_rsc:resource_id(), z:context() ) -> boolean().
is_allowed_set_username(Id, Context) when is_integer(Id) ->
Expand Down Expand Up @@ -1023,8 +1074,7 @@ check_hash(RscId, Username, Password, Hash, Context) ->
end.


check_hash_ok(RscId, Context) ->
set_visited(RscId, Context),
check_hash_ok(RscId, _Context) ->
{ok, RscId}.

%% @doc Prevent insert of reserved usernames.
Expand Down
1 change: 1 addition & 0 deletions src/support/z_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ logon(UserId, Context) ->
Context3 = z_session:ensure_page_session(Context2),
Context4 = z_notifier:foldl(auth_logon, Context3, Context3),
z_notifier:notify(auth_logon_done, Context4),
m_identity:set_visited(UserId, Context),
{ok, Context4};
false ->
{error, user_not_enabled}
Expand Down

0 comments on commit 90db6f2

Please sign in to comment.