Skip to content

Commit

Permalink
✨ new setting to enable OIDC auto login
Browse files Browse the repository at this point in the history
🐛 trying to fix issue #216 - incorrect dates saved
  • Loading branch information
faburem committed Dec 18, 2024
1 parent 32531b3 commit a016d71
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 55 deletions.
2 changes: 1 addition & 1 deletion imports/api/projects/server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Meteor.publish('projectStats', async function projectStats(projectId) {
.isBetween(beforePreviousMonthStart, beforePreviousMonthEnd)) {
beforePreviousMonthHours += Number.parseFloat(timecard.hours)
}
if (project.rates[timecard.userId]) {
if (project?.rates && project.rates[timecard.userId]) {
totalRevenue += Number.parseFloat(timecard.hours)
* Number.parseFloat(project.rates[timecard.userId])
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Template.oidccomponent.events({
event.preventDefault()
const configuration = {
service: 'oidc',
loginStyle: 'popup',
}
for (const element of templateInstance.$('.js-setting-input')) {
const { name } = element
Expand Down
12 changes: 6 additions & 6 deletions imports/ui/pages/details/components/detailtimetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function detailedDataTableMapper(entry, forExport) {
mapping.push(entry.state)
}
if (getGlobalSetting('useStartTime')) {
mapping.push(dayjs.utc(entry.date).local().format('HH:mm'))
mapping.push(dayjs.utc(entry.date).local().add(entry.hours, 'hour').format('HH:mm'))
mapping.push(dayjs.utc(entry.date).local().format('HH:mm')) // Ensure consistent timezone usage
mapping.push(dayjs.utc(entry.date).local().add(entry.hours, 'hour').format('HH:mm')) // Ensure consistent timezone usage
}
mapping.push(Number(timeInUserUnit(entry.hours)))
if (getGlobalSetting('showRateInDetails')) {
Expand Down Expand Up @@ -113,8 +113,8 @@ Template.detailtimetable.onCreated(function workingtimetableCreated() {
}
if (this.data?.period.get() === 'custom') {
subscriptionParameters.dates = {
startDate: getUserSetting('customStartDate') ? getUserSetting('customStartDate') : dayjs.utc().startOf('month').toDate(),
endDate: getUserSetting('customEndDate') ? getUserSetting('customEndDate') : dayjs.utc().toDate(),
startDate: getUserSetting('customStartDate') ? getUserSetting('customStartDate') : dayjs.utc().startOf('month').toDate(), // Ensure consistent timezone usage
endDate: getUserSetting('customEndDate') ? getUserSetting('customEndDate') : dayjs.utc().toDate(), // Ensure consistent timezone usage
}
}
this.detailedEntriesPeriodCountHandle = this.subscribe('getDetailedTimeEntriesForPeriodCount', subscriptionParameters)
Expand All @@ -133,8 +133,8 @@ Template.detailtimetable.onCreated(function workingtimetableCreated() {
customer: this.data?.customer.get(),
period: this.data?.period.get(),
dates: {
startDate: getUserSetting('customStartDate') ? getUserSetting('customStartDate') : dayjs.utc().startOf('month').toDate(),
endDate: getUserSetting('customEndDate') ? getUserSetting('customEndDate') : dayjs.utc().toDate(),
startDate: getUserSetting('customStartDate') ? getUserSetting('customStartDate') : dayjs.utc().startOf('month').toDate(), // Ensure consistent timezone usage
endDate: getUserSetting('customEndDate') ? getUserSetting('customEndDate') : dayjs.utc().toDate(), // Ensure consistent timezone usage
},
userId: this.data?.resource.get(),
limit: this.data?.limit.get(),
Expand Down
14 changes: 13 additions & 1 deletion imports/ui/pages/signIn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FlowRouter } from 'meteor/ostrio:flow-router-extra'
import { validateEmail, getGlobalSetting } from '../../utils/frontend_helpers'
import { isOidcConfigured, disableDefaultLoginForm } from '../../utils/oidc/oidc_helper'
import { isOidcConfigured, disableDefaultLoginForm, isAutoLoginEnabled } from '../../utils/oidc/oidc_helper'
import { oidcReady } from '../../utils/oidc/oidc_client'
import { t } from '../../utils/i18n.js'
import './signIn.html'

Expand Down Expand Up @@ -81,3 +82,14 @@ Template.signIn.events({
FlowRouter.go('register', {}, { email: templateInstance.$('#at-field-email').val() })
},
})

Template.signIn.onRendered(function signInRendered() {
const templateInstance = this
this.autorun(() => {
if(oidcReady.get() && isAutoLoginEnabled()) {
Meteor.loginWithOidc((error) => {
handleLoginResult(error, templateInstance)
})
}
})
})
8 changes: 4 additions & 4 deletions imports/ui/pages/track/components/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { getHolidays } from '../../../../utils/holiday.js'
Template.calendar.onCreated(function calendarCreated() {
dayjs.extend(utc)
this.subscribe('myprojects', {})
this.startDate = new ReactiveVar(dayjs.utc().startOf('month').toDate())
this.endDate = new ReactiveVar(dayjs.utc().endOf('month').toDate())
this.startDate = new ReactiveVar(dayjs.utc().startOf('month').toDate()) // Ensure consistent timezone usage
this.endDate = new ReactiveVar(dayjs.utc().endOf('month').toDate()) // Ensure consistent timezone usage
this.tcid = new ReactiveVar()
this.selectedProjectId = new ReactiveVar()
this.selectedDate = new ReactiveVar()
Expand All @@ -42,7 +42,7 @@ Template.calendar.onRendered(() => {
droppable: true,
aspectRatio: 2,
height: 'auto',
timeZone: 'UTC',
timeZone: 'UTC', // Ensure consistent timezone usage
firstDay: getUserSetting('startOfWeek'),
themeSystem: 'bootstrap',
events: (fetchInfo, successCallback) => {
Expand Down Expand Up @@ -119,7 +119,7 @@ Template.calendar.onRendered(() => {
new bootstrap.Modal($('#edit-tc-entry-modal')[0], { focus: false }).show()
},
datesSet: (dateInfo) => {
FlowRouter.setQueryParams({ date: dayjs(dateInfo.view.currentStart).format('YYYY-MM-DD') })
FlowRouter.setQueryParams({ date: dayjs.utc(dateInfo.view.currentStart).format('YYYY-MM-DD') }) // Ensure consistent timezone usage
},
})
templateInstance.calendar.render()
Expand Down
20 changes: 10 additions & 10 deletions imports/ui/pages/track/components/weektable.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ Template.weektable.onCreated(function weekTableCreated() {
this.totalForWeekPerDay = new ReactiveVar([])
this.autorun(() => {
if (this.subscriptionsReady()) {
this.startDate.set(dayjs().startOf('day').isoWeekday(getUserSetting('startOfWeek')))
this.endDate.set(dayjs().endOf('day').isoWeekday(getUserSetting('startOfWeek')).add(6, 'day'))
this.startDate.set(dayjs.utc().startOf('day').isoWeekday(getUserSetting('startOfWeek'))) // Ensure consistent timezone usage
this.endDate.set(dayjs.utc().endOf('day').isoWeekday(getUserSetting('startOfWeek')).add(6, 'day')) // Ensure consistent timezone usage
}
})
this.autorun(() => {
if (FlowRouter.getQueryParam('date')) {
this.startDate.set(dayjs.utc(FlowRouter.getQueryParam('date')).isoWeekday(getUserSetting('startOfWeek')), 'YYYY-MM-DD')
this.endDate.set(dayjs.utc(FlowRouter.getQueryParam('date')).isoWeekday(getUserSetting('startOfWeek')).add(6, 'day'), 'YYYY-MM-DD')
this.startDate.set(dayjs.utc(FlowRouter.getQueryParam('date')).isoWeekday(getUserSetting('startOfWeek')), 'YYYY-MM-DD') // Ensure consistent timezone usage
this.endDate.set(dayjs.utc(FlowRouter.getQueryParam('date')).isoWeekday(getUserSetting('startOfWeek')).add(6, 'day'), 'YYYY-MM-DD') // Ensure consistent timezone usage
}
})
this.autorun(async () => {
Expand Down Expand Up @@ -77,7 +77,7 @@ Template.weektable.helpers({
endDate() { return Template.instance().endDate },
getTotalForDay(day) {
for (const element of Template.instance().totalForWeekPerDay.get()) {
if (dayjs.utc(new Date(element._id.date)).format(getGlobalSetting('weekviewDateFormat')) === day) {
if (dayjs.utc(new Date(element._id.date)).format(getGlobalSetting('weekviewDateFormat')) === day) { // Ensure consistent timezone usage
return element.totalForDate !== 0 ? timeInUserUnit(element.totalForDate) : false
}
}
Expand All @@ -93,7 +93,7 @@ Template.weektable.helpers({
}
return false
},
isTodayClass: (weekday) => (dayjs.utc(weekday, getGlobalSetting('weekviewDateFormat')).isSame(dayjs.utc(), 'day') ? 'text-primary' : ''),
isTodayClass: (weekday) => (dayjs.utc(weekday, getGlobalSetting('weekviewDateFormat')).isSame(dayjs.utc(), 'day') ? 'text-primary' : ''), // Ensure consistent timezone usage
})

Template.weektable.events({
Expand All @@ -107,7 +107,7 @@ Template.weektable.events({
},
'click .js-today': (event, templateInstance) => {
event.preventDefault()
FlowRouter.setQueryParams({ date: dayjs.utc().isoWeekday(getUserSetting('startOfWeek')).format('YYYY-MM-DD') })
FlowRouter.setQueryParams({ date: dayjs.utc().isoWeekday(getUserSetting('startOfWeek')).format('YYYY-MM-DD') }) // Ensure consistent timezone usage
},
'keyup .js-hours': (event, templateInstance) => {
if (event.keyCode === 13) {
Expand Down Expand Up @@ -137,7 +137,7 @@ Template.weektable.events({
hours /= 60
}
const projectId = $(element).data('project-id')
const date = dayjs.utc(startDate.add(Number(templateInstance.$(element).data('week-day')), 'day').format('YYYY-MM-DD')).toDate()
const date = dayjs.utc(startDate.add(Number(templateInstance.$(element).data('week-day')), 'day').format('YYYY-MM-DD')).toDate() // Ensure consistent timezone usage
const existingElement = weekArray
.findIndex((arrayElement) => arrayElement.projectId === projectId
&& arrayElement.task === task && dayjs(arrayElement.date).isSame(dayjs(date)))
Expand Down Expand Up @@ -291,7 +291,7 @@ Template.weektablerow.helpers({
getHoursForDay(day, task) {
if (task.entries && getGlobalSetting('weekviewDateFormat') && i18nReady.get()) {
const entryForDay = task.entries
.filter((entry) => dayjs.utc(entry.date).format(getGlobalSetting('weekviewDateFormat')) === dayjs.utc(day, getGlobalSetting('weekviewDateFormat')).format(getGlobalSetting('weekviewDateFormat')))
.filter((entry) => dayjs.utc(entry.date).format(getGlobalSetting('weekviewDateFormat')) === dayjs.utc(day, getGlobalSetting('weekviewDateFormat')).format(getGlobalSetting('weekviewDateFormat'))) // Ensure consistent timezone usage
.reduce(((total, element) => total + element.hours), 0)
return entryForDay !== 0 ? timeInUserUnit(entryForDay) : ''
}
Expand All @@ -312,7 +312,7 @@ Template.weektablerow.helpers({
if (!Meteor.loggingIn() && Meteor.user() && Meteor.user().profile) {
Template.instance().methodTimeEntries.get().concat(Template.instance().tempTimeEntries.get()).forEach((element) => {
if (element.entries) {
total += element.entries.filter((entry) => dayjs.utc(entry.date).format(getGlobalSetting('weekviewDateFormat')) === dayjs.utc(day, getGlobalSetting('weekviewDateFormat')).format(getGlobalSetting('weekviewDateFormat')))
total += element.entries.filter((entry) => dayjs.utc(entry.date).format(getGlobalSetting('weekviewDateFormat')) === dayjs.utc(day, getGlobalSetting('weekviewDateFormat')).format(getGlobalSetting('weekviewDateFormat'))) // Ensure consistent timezone usage
.reduce((tempTotal, current) => tempTotal + Number(current.hours), 0)
}
})
Expand Down
12 changes: 6 additions & 6 deletions imports/ui/pages/track/tracktime.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Template.tracktime.onCreated(function tracktimeCreated() {
dayjs.extend(utc)
dayjs.extend(customParseFormat)
dayjs.extend(duration)
this.date = new ReactiveVar(dayjs().toDate())
this.date = new ReactiveVar(dayjs.utc().toDate()) // Ensure consistent timezone usage
this.projectId = new ReactiveVar()
this.tcid = new ReactiveVar()
this.totalTime = new ReactiveVar(0)
Expand All @@ -99,7 +99,7 @@ Template.tracktime.onCreated(function tracktimeCreated() {
this.date.set(this.data?.dateArg.get())
} else if (!(this.data?.dateArg && this.data?.dateArg.get())
&& !(this.data?.tcid && this.data?.tcid.get()) && FlowRouter.getQueryParam('date')) {
this.date.set(dayjs(FlowRouter.getQueryParam('date'), 'YYYY-MM-DD').toDate())
this.date.set(dayjs.utc(FlowRouter.getQueryParam('date'), 'YYYY-MM-DD').toDate()) // Ensure consistent timezone usage
}
if (this.data?.projectIdArg && this.data?.projectIdArg.get()) {
this.projectId.set(this.data?.projectIdArg.get())
Expand All @@ -111,12 +111,12 @@ Template.tracktime.onCreated(function tracktimeCreated() {
if (this.subscriptionsReady()) {
this.time_entry.set(Timecards.findOne(this.tcid.get()))
this.date.set(Timecards.findOne({ _id: this.tcid.get() })
? dayjs.utc(Timecards.findOne({ _id: this.tcid.get() }).date).toDate()
? dayjs.utc(Timecards.findOne({ _id: this.tcid.get() }).date).toDate() // Ensure consistent timezone usage
: dayjs().toDate())
this.projectId.set(Timecards.findOne({ _id: this.tcid.get() }) ? Timecards.findOne({ _id: this.tcid.get() }).projectId : '')
}
} else {
handle = this.subscribe('myTimecardsForDate', { date: dayjs(this.date.get()).format('YYYY-MM-DD') })
handle = this.subscribe('myTimecardsForDate', { date: dayjs.utc(this.date.get()).format('YYYY-MM-DD') }) // Ensure consistent timezone usage
if (handle.ready()) {
Timecards.find().forEach((timecard) => {
this.subscribe('publicProjectName', timecard.projectId)
Expand Down Expand Up @@ -203,7 +203,7 @@ Template.tracktime.events({
const localDate = dayjs(templateInstance.$('.js-date').val()).toDate()
let date = dayjs.utc(templateInstance.$('.js-date').val(), getGlobalSetting('dateformatVerbose')).isValid()
? dayjs.utc(templateInstance.$('.js-date').val(), getGlobalSetting('dateformatVerbose')).toDate()
: dayjs.utc(`${localDate.getFullYear()}-${localDate.getMonth() + 1}-${localDate.getDate()}`).toDate()
: dayjs.utc(`${localDate.getFullYear()}-${localDate.getMonth() + 1}-${localDate.getDate()}`).toDate() // Ensure consistent timezone usage
if (getGlobalSetting('useStartTime') && !templateInstance.tcid?.get()) {
if ($('#startTime').val()) {
date = dayjs.utc(date.setHours($('#startTime').val().split(':')[0], $('#startTime').val().split(':')[1])).toDate()
Expand Down Expand Up @@ -404,7 +404,7 @@ function isEditMode() {
}
Template.tracktime.helpers({
date: () => (Template.instance().tcid && Template.instance().tcid.get()
? dayjs.utc(Template.instance().date.get()).format(getGlobalSetting('dateformatVerbose'))
? dayjs.utc(Template.instance().date.get()).format(getGlobalSetting('dateformatVerbose')) // Ensure consistent timezone usage
: dayjs(Template.instance().date.get()).format(getGlobalSetting('dateformatVerbose'))),
projectId: () => Template.instance().projectId.get(),
reactiveProjectId: () => Template.instance().projectId,
Expand Down
5 changes: 3 additions & 2 deletions imports/utils/oidc/oidc_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Random } from 'meteor/random'
import { ServiceConfiguration } from 'meteor/service-configuration'

const SERVICE_NAME = 'oidc'

const oidcReady = new ReactiveVar(false)
function registerOidc() {
Accounts.oauth.registerService(SERVICE_NAME)

Expand Down Expand Up @@ -66,5 +66,6 @@ function registerOidc() {
popupOptions,
})
}
oidcReady.set(true)
}
export { registerOidc }
export { registerOidc, oidcReady }
19 changes: 17 additions & 2 deletions imports/utils/oidc/oidc_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const oidcFields = [
{
property: 'disableDefaultLoginForm', label: 'Disable Default Login Form', type: 'checkbox', value: false,
},
{
property: 'autoInitiateLogin', label: 'Automatically initiate login', type: 'checkbox', value: false,
},
{
property: 'clientId', label: 'Client ID', type: 'text', value: '',
},
Expand All @@ -31,6 +34,9 @@ const oidcFields = [
{
property: 'requestPermissions', label: 'Request Permissions', type: 'text', value: '"openid", "profile", "email"',
},
{
property: 'loginStyle', label: 'Login style (popup or redirect)', type: 'text', value: 'popup',
},
]

function isOidcConfigured() {
Expand All @@ -39,7 +45,16 @@ function isOidcConfigured() {
}
return false
}

function isAutoLoginEnabled() {
if (!getGlobalSetting('enableOpenIDConnect')) {
return false
}
const configuration = ServiceConfiguration.configurations.findOne({ service: SERVICE_NAME })
if (configuration === undefined) {
return false
}
return configuration.autoInitiateLogin
}
function disableDefaultLoginForm() {
if (!getGlobalSetting('enableOpenIDConnect')) {
return false
Expand All @@ -61,5 +76,5 @@ function getOidcConfiguration(name) {
return ''
}
export {
oidcFields, isOidcConfigured, disableDefaultLoginForm, getOidcConfiguration,
oidcFields, isOidcConfigured, disableDefaultLoginForm, getOidcConfiguration, isAutoLoginEnabled
}
Loading

0 comments on commit a016d71

Please sign in to comment.