Skip to content

Commit

Permalink
fix(auth): refactor current user handling into service
Browse files Browse the repository at this point in the history
Writing instances of JS classes directly to the `data` property
of the ember simple auth service is problematic, because ember
simple auth always saves the _serialized_ form of the class in
local storage. In our case, this caused issues when the application
is open in multiple tabs, and on tab triggers a token refresh: In that
case, the initialization of the class instance is not triggered, and the
serialized data is read directly from local storage.

This refactors the current implementation to use a separate service, as
suggested by the ember-simple-auth docs:
https://github.com/mainmatter/ember-simple-auth/blob/master/guides/managing-current-user.md
  • Loading branch information
czosel authored and c0rydoras committed May 14, 2024
1 parent 4b386f1 commit ea7910b
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 125 deletions.
4 changes: 2 additions & 2 deletions frontend/app/abilities/absence-credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject as service } from "@ember/service";
import { Ability } from "ember-can";

export default class AbsenceCreditAbility extends Ability {
@service session;
@service currentUser;

get user() {
return this.session.data.user;
return this.currentUser.user;
}
get canEdit() {
return this.user.isSuperuser;
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/abilities/overtime-credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject as service } from "@ember/service";
import { Ability } from "ember-can";

export default class OvertimeCreditAbility extends Ability {
@service session;
@service currentUser;

get user() {
return this.session.data.user;
return this.currentUser.user;
}
get canEdit() {
return this.user.isSuperuser;
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/abilities/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject as service } from "@ember/service";
import { Ability } from "ember-can";

export default class PageAbility extends Ability {
@service session;
@service currentUser;

get user() {
return this.session.data.user;
return this.currentUser.user;
}
get canAccess() {
if (!this.user) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/abilities/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject as service } from "@ember/service";
import { Ability } from "ember-can";

export default class ReportAbility extends Ability {
@service session;
@service currentUser;

get user() {
return this.session.data.user;
return this.currentUser.user;
}

get canEdit() {
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/abilities/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { inject as service } from "@ember/service";
import { Ability } from "ember-can";

export default class UserAbility extends Ability {
@service session;
@service currentUser;

get user() {
return this.session.data.user;
return this.currentUser.user;
}

get canRead() {
Expand Down
10 changes: 5 additions & 5 deletions frontend/app/analysis/edit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class AnalysisEditController extends Controller {
@service notify;
@service router;
@service fetch;
@service session;
@service currentUser;
@service store;
@service unverifiedReports;

Expand Down Expand Up @@ -86,11 +86,11 @@ export default class AnalysisEditController extends Controller {
}

get isAccountant() {
return this.session.data.user.isAccountant;
return this.currentUser.user.isAccountant;
}

get isSuperuser() {
return this.session.data.user.isSuperuser;
return this.currentUser.user.isSuperuser;
}

@task
Expand Down Expand Up @@ -130,11 +130,11 @@ export default class AnalysisEditController extends Controller {
}

get hasSelectedOwnReports() {
return this.intersectionModel.user.get("id") === this.session.data.user.id;
return this.intersectionModel.user.get("id") === this.currentUser.user.id;
}

get isReviewer() {
return allQueryParams(this).reviewer === this.session.data.user.id;
return allQueryParams(this).reviewer === this.currentUser.user.id;
}

get canVerify() {
Expand Down
3 changes: 2 additions & 1 deletion frontend/app/analysis/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class AnalysisController extends QPController {
exportLimit = config.APP.EXPORT_LIMIT;

@service session;
@service currentUser;
@service store;
@service router;
@service notify;
Expand Down Expand Up @@ -119,7 +120,7 @@ export default class AnalysisController extends QPController {

get canBill() {
return (
this.session.data.user.isAccountant || this.session.data.user.isSuperuser
this.currentUser.user.isAccountant || this.currentUser.user.isSuperuser
);
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/components/report-review-warning/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inject as service } from "@ember/service";
import Component from "@glimmer/component";

export default class ReportReviewWarning extends Component {
@service session;
@service currentUser;

@service unverifiedReports;

Expand Down
4 changes: 2 additions & 2 deletions frontend/app/components/report-review-warning/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
toDate=(moment-format
this.unverifiedReports.reportsToDate "YYYY-MM-DD"
)
reviewer=this.session.data.user.id
reviewer=this.currentUser.user.id
editable=1
rejected=null
verified=0
Expand All @@ -34,7 +34,7 @@
@query={{hash
fromDate=null
toDate=null
user=this.session.data.user.id
user=this.currentUser.user.id
editable=1
rejected=1
verified=0
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/components/sy-topnav/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class SyTopnav extends Component {
@service session;
@service currentUser;

@service media;

Expand Down
6 changes: 3 additions & 3 deletions frontend/app/components/sy-topnav/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</li>
{{/if}}
{{/unless}}
{{#if this.session.data.user.isSuperuser}}
{{#if this.currentUser.user.isSuperuser}}
<li class="nav-top-list-item">
<LinkTo @route="users.index">
<FaIcon @icon="users" @prefix="fas" />
Expand All @@ -59,9 +59,9 @@
<ul class="nav-top-list nav-top-list--right">
<ReportReviewWarning />
<li class="nav-top-list-item">
<LinkTo @route="users.edit" @model={{this.session.data.user.id}}>
<LinkTo @route="users.edit" @model={{this.currentUser.user.id}}>
<FaIcon @icon="user" />
{{this.session.data.user.fullName}}
{{this.currentUser.user.fullName}}
</LinkTo>
</li>
<li class="nav-top-list-item">
Expand Down
3 changes: 2 additions & 1 deletion frontend/app/index/activities/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class ActivitiesIndexController extends Controller {
@service store;
@service notify;
@service tracking;
@service currentUser;

@tracked showUnknownWarning = false;
@tracked showOverlappingWarning = false;
Expand Down Expand Up @@ -58,7 +59,7 @@ export default class ActivitiesIndexController extends Controller {
return (
activity.get("date") &&
activity.get("date").isSame(this.model, "day") &&
activity.get("user.id") === this.user.id &&
activity.get("user.id") === this.currentUser.user.id &&
!activity.get("isNew") &&
!activity.get("isDeleted")
);
Expand Down
6 changes: 0 additions & 6 deletions frontend/app/index/activities/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ export default class IndexActivitiesRoute extends Route {
model() {
return this.modelFor("index");
}

setupController(controller, ...args) {
super.setupController(controller, ...args);

controller.set("user", this.modelFor("protected"));
}
}
3 changes: 2 additions & 1 deletion frontend/app/index/attendances/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class AttendanceController extends Controller {
@service notify;
@service store;
@service tracking;
@service currentUser;

AttendanceValidator = AttendanceValidator;

Expand Down Expand Up @@ -53,7 +54,7 @@ export default class AttendanceController extends Controller {
return this._allAttendances.filter((a) => {
return (
a.get("date").isSame(this.model, "day") &&
a.get("user.id") === this.user.id &&
a.get("user.id") === this.currentUser.user.id &&
!a.get("isDeleted")
);
});
Expand Down
15 changes: 1 addition & 14 deletions frontend/app/index/attendances/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,4 @@ import Route from "@ember/routing/route";
* @extends Ember.Route
* @public
*/
export default class AttendaceIndexRoute extends Route {
/**
* Setup controller hook, set the current user
*
* @method setupContrller
* @param {Ember.Controller} controller The controller
* @public
*/
setupController(controller, ...args) {
super.setupController(controller, ...args);

controller.set("user", this.modelFor("protected"));
}
}
export default class AttendaceIndexRoute extends Route {}
19 changes: 10 additions & 9 deletions frontend/app/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class IndexController extends Controller {
@service store;
@service notify;
@service tracking;
@service currentUser;

AbsenceValidations = AbsenceValidations;
MultipleAbsenceValidations = MultipleAbsenceValidations;
Expand All @@ -55,7 +56,7 @@ export default class IndexController extends Controller {
return (
a.get("date") &&
a.get("date").isSame(this.date, "day") &&
a.get("user.id") === this.user?.id &&
a.get("user.id") === this.currentUser.user?.id &&
!a.get("isDeleted")
);
});
Expand Down Expand Up @@ -166,7 +167,7 @@ export default class IndexController extends Controller {
return (
attendance.get("date") &&
attendance.get("date").isSame(this.date, "day") &&
attendance.get("user.id") === this.user?.id &&
attendance.get("user.id") === this.currentUser.user?.id &&
!attendance.get("isDeleted")
);
});
Expand Down Expand Up @@ -214,7 +215,7 @@ export default class IndexController extends Controller {
return this.allReports.filter((report) => {
return (
report.date.isSame(this.date, "day") &&
report.get("user.id") === this.user?.id &&
report.get("user.id") === this.currentUser.user?.id &&
!report.isNew &&
!report.isDeleted
);
Expand All @@ -231,7 +232,7 @@ export default class IndexController extends Controller {
return this.allAbsences.filter((absence) => {
return (
absence.date.isSame(this.date, "day") &&
absence.get("user.id") === this.user?.id &&
absence.get("user.id") === this.currentUser.user?.id &&
!absence.isNew &&
!absence.isDeleted
);
Expand Down Expand Up @@ -301,7 +302,7 @@ export default class IndexController extends Controller {
* @public
*/
get expectedWorktime() {
return this.user.activeEmployment.worktimePerDay;
return this.currentUser.user.activeEmployment.worktimePerDay;
}

/**
Expand All @@ -312,7 +313,7 @@ export default class IndexController extends Controller {
*/
get workdays() {
// eslint-disable-next-line ember/no-get
return get(this, "user.activeEmployment.location.workdays");
return get(this, "currentUser.user.activeEmployment.location.workdays");
}

/**
Expand All @@ -324,14 +325,14 @@ export default class IndexController extends Controller {
weeklyOverviewData = trackedFunction(this, {}, async () => {
const allReports = this.allReports.filter(
(report) =>
report.get("user.id") === this.user.get("id") &&
report.get("user.id") === this.currentUser.user.get("id") &&
!report.get("isDeleted") &&
!report.get("isNew")
);

const allAbsences = this.allAbsences.filter(
(absence) =>
absence.get("user.id") === this.user.get("id") &&
absence.get("user.id") === this.currentUser.user.get("id") &&
!absence.get("isDeleted") &&
!absence.get("isNew")
);
Expand Down Expand Up @@ -420,7 +421,7 @@ export default class IndexController extends Controller {
const params = {
from_date: from.format("YYYY-MM-DD"),
to_date: to.format("YYYY-MM-DD"),
user: this.user?.id,
user: this.currentUser.user?.id,
};

const absences = yield this.store.query("absence", params);
Expand Down
7 changes: 4 additions & 3 deletions frontend/app/index/reports/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default class IndexReportController extends Controller {
@service store;
@service notify;
@service router;
@service currentUser;

ReportValidations = ReportValidations;

Expand Down Expand Up @@ -65,7 +66,7 @@ export default class IndexReportController extends Controller {
get reports() {
const reportsToday = this._allReports.filter((r) => {
return (
(!r.get("user.id") || r.get("user.id") === this.user.id) &&
(!r.get("user.id") || r.get("user.id") === this.currentUser.user.id) &&
r.get("date").isSame(this.model, "day") &&
!r.get("isDeleted")
);
Expand All @@ -74,7 +75,7 @@ export default class IndexReportController extends Controller {
if (!reportsToday.filterBy("isNew", true).get("length")) {
this.store.createRecord("report", {
date: this.model,
user: this.user,
user: this.currentUser.user,
});
}

Expand All @@ -86,7 +87,7 @@ export default class IndexReportController extends Controller {
const absences = this.store.peekAll("absence").filter((absence) => {
return (
absence.date.isSame(this.model, "day") &&
absence.get("user.id") === this.user.id &&
absence.get("user.id") === this.currentUser.user.id &&
!absence.isNew &&
!absence.isDeleted
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/index/reports/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import moment from "moment";

export default class IndexReportsRoute extends Route {
@service store;
@service currentUser;
@service notify;

/**
Expand All @@ -22,7 +23,6 @@ export default class IndexReportsRoute extends Route {
async setupController(controller, model, ...args) {
super.setupController(controller, model, ...args);

controller.set("user", this.modelFor("protected"));
controller.set("rescheduleDate", model);

if (controller.task) {
Expand All @@ -36,7 +36,7 @@ export default class IndexReportsRoute extends Route {
: "",
date: model,
comment: controller.comment ?? "",
user: this.modelFor("protected"),
user: this.currentUser.user,
review: controller.review ?? false,
notBillable: controller.notBillable ?? false,
remainingEffort: task.mostRecentRemainingEffort,
Expand Down
Loading

0 comments on commit ea7910b

Please sign in to comment.