core: fix user metrics not accepting detail

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-03 20:33:59 +02:00
parent 4554c468bc
commit e246071aac
4 changed files with 48 additions and 30 deletions

View File

@ -61,23 +61,23 @@ class UserMetricsSerializer(PassiveSerializer):
@swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True)) @swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True))
def get_logins_per_1h(self, _): def get_logins_per_1h(self, _):
"""Get successful logins per hour for the last 24 hours""" """Get successful logins per hour for the last 24 hours"""
request = self.context["request"]._request user = self.context["user"]
return get_events_per_1h(action=EventAction.LOGIN, user__pk=request.user.pk) return get_events_per_1h(action=EventAction.LOGIN, user__pk=user.pk)
@swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True)) @swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True))
def get_logins_failed_per_1h(self, _): def get_logins_failed_per_1h(self, _):
"""Get failed logins per hour for the last 24 hours""" """Get failed logins per hour for the last 24 hours"""
request = self.context["request"]._request user = self.context["user"]
return get_events_per_1h( return get_events_per_1h(
action=EventAction.LOGIN_FAILED, context__username=request.user.username action=EventAction.LOGIN_FAILED, context__username=user.username
) )
@swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True)) @swagger_serializer_method(serializer_or_field=CoordinateSerializer(many=True))
def get_authorizations_per_1h(self, _): def get_authorizations_per_1h(self, _):
"""Get failed logins per hour for the last 24 hours""" """Get failed logins per hour for the last 24 hours"""
request = self.context["request"]._request user = self.context["user"]
return get_events_per_1h( return get_events_per_1h(
action=EventAction.AUTHORIZE_APPLICATION, user__pk=request.user.pk action=EventAction.AUTHORIZE_APPLICATION, user__pk=user.pk
) )
@ -109,11 +109,13 @@ class UserViewSet(ModelViewSet):
@permission_required("authentik_core.view_user", ["authentik_events.view_event"]) @permission_required("authentik_core.view_user", ["authentik_events.view_event"])
@swagger_auto_schema(responses={200: UserMetricsSerializer(many=False)}) @swagger_auto_schema(responses={200: UserMetricsSerializer(many=False)})
@action(detail=False, pagination_class=None, filter_backends=[]) @action(detail=True, pagination_class=None, filter_backends=[])
def metrics(self, request: Request) -> Response: # pylint: disable=invalid-name, unused-argument
def metrics(self, request: Request, pk: int) -> Response:
"""User metrics per 1h""" """User metrics per 1h"""
user: User = self.get_object()
serializer = UserMetricsSerializer(True) serializer = UserMetricsSerializer(True)
serializer.context["request"] = request serializer.context["user"] = user
return Response(serializer.data) return Response(serializer.data)
@permission_required("authentik_core.reset_user_password") @permission_required("authentik_core.reset_user_password")
@ -135,3 +137,4 @@ class UserViewSet(ModelViewSet):
reverse_lazy("authentik_flows:default-recovery") + f"?{querystring}" reverse_lazy("authentik_flows:default-recovery") + f"?{querystring}"
) )
return Response({"link": link}) return Response({"link": link})

View File

@ -2087,23 +2087,6 @@ paths:
tags: tags:
- core - core
parameters: [] parameters: []
/core/users/metrics/:
get:
operationId: core_users_metrics
description: User metrics per 1h
parameters: []
responses:
'200':
description: ''
schema:
$ref: '#/definitions/UserMetrics'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
tags:
- core
parameters: []
/core/users/{id}/: /core/users/{id}/:
get: get:
operationId: core_users_read operationId: core_users_read
@ -2207,6 +2190,33 @@ paths:
description: A unique integer value identifying this User. description: A unique integer value identifying this User.
required: true required: true
type: integer type: integer
/core/users/{id}/metrics/:
get:
operationId: core_users_metrics
description: User metrics per 1h
parameters: []
responses:
'200':
description: ''
schema:
$ref: '#/definitions/UserMetrics'
'403':
description: Authentication credentials were invalid, absent or insufficient.
schema:
$ref: '#/definitions/GenericError'
'404':
description: Object does not exist or caller has insufficient permissions
to access it.
schema:
$ref: '#/definitions/APIException'
tags:
- core
parameters:
- name: id
in: path
description: A unique integer value identifying this User.
required: true
type: integer
/core/users/{id}/recovery/: /core/users/{id}/recovery/:
get: get:
operationId: core_users_recovery operationId: core_users_recovery

View File

@ -1,4 +1,4 @@
import { customElement } from "lit-element"; import { customElement, property } from "lit-element";
import Chart from "chart.js"; import Chart from "chart.js";
import { CoreApi, UserMetrics } from "authentik-api"; import { CoreApi, UserMetrics } from "authentik-api";
import { AKChart } from "./Chart"; import { AKChart } from "./Chart";
@ -7,8 +7,13 @@ import { DEFAULT_CONFIG } from "../../api/Config";
@customElement("ak-charts-user") @customElement("ak-charts-user")
export class UserChart extends AKChart<UserMetrics> { export class UserChart extends AKChart<UserMetrics> {
@property()
userId?: number;
apiRequest(): Promise<UserMetrics> { apiRequest(): Promise<UserMetrics> {
return new CoreApi(DEFAULT_CONFIG).coreUsersMetrics(); return new CoreApi(DEFAULT_CONFIG).coreUsersMetrics({
id: this.userId || 0,
});
} }
getDatasets(data: UserMetrics): Chart.ChartDataSets[] { getDatasets(data: UserMetrics): Chart.ChartDataSets[] {

View File

@ -167,7 +167,7 @@ export class UserViewPage extends Page {
</div> </div>
<div class="pf-c-card pf-l-gallery__item pf-m-4-col" style="grid-column-end: span 4;grid-row-end: span 2;"> <div class="pf-c-card pf-l-gallery__item pf-m-4-col" style="grid-column-end: span 4;grid-row-end: span 2;">
<div class="pf-c-card__body"> <div class="pf-c-card__body">
<ak-charts-user> <ak-charts-user userId=${this.user.pk}>
</ak-charts-user> </ak-charts-user>
</div> </div>
</div> </div>
@ -177,7 +177,7 @@ export class UserViewPage extends Page {
<div class="pf-c-card"> <div class="pf-c-card">
<div class="pf-c-card__body"> <div class="pf-c-card__body">
<ak-object-changelog <ak-object-changelog
targetModelPk=${this.user.pk || ""} targetModelPk=${this.user.pk || 0}
targetModelApp="authentik_core" targetModelApp="authentik_core"
targetModelName="user"> targetModelName="user">
</ak-object-changelog> </ak-object-changelog>