Esempio n. 1
0
        public async Task Invoke(HttpContext context)
        {
            TrackerClientSession clientSession = new TrackerClientSession();

            if (context.Request.Cookies.TryGetValue(_serviceOptions.CookieName, out var cookie))
            {
                try
                {
                    JsonConvert.PopulateObject(cookie, clientSession);
                    // restore managed code objects
                    clientSession.DeviceTimeZone = clientSession.DeviceTimeZoneCookieValue != null?TimeZoneInfo.FindSystemTimeZoneById(clientSession.DeviceTimeZoneCookieValue) : null;

                    clientSession.ViewTimeZone = clientSession.ViewTimeZoneCookieValue != null?TimeZoneInfo.FindSystemTimeZoneById(clientSession.ViewTimeZoneCookieValue) : null;

                    if (clientSession.Revision < TrackerClientSession.TargetRevision)
                    {
                        clientSession.UpdateTrackerOptionsCookie(context.Response, _serviceOptions.CookieName);
                    }
                }
                catch (JsonSerializationException)
                {
                    // bad cookie format
                    context.Response.Cookies.Delete(_serviceOptions.CookieName);
                    clientSession = new TrackerClientSession();
                }
            }
            context.Features.Set <TrackerClientSession>(clientSession);

            await this._next.Invoke(context);
        }
Esempio n. 2
0
        public TimeTableViewModel(TrackerClientSession clientSession, TimeTable timeTableAndWorkUnits, string tableOwnerUserName)
        {
            TableOwnerUserName    = tableOwnerUserName;
            TimeTableAndWorkUnits = timeTableAndWorkUnits.TranformTrackedTimesForView(clientSession);

            var tableDateRange = timeTableAndWorkUnits.GetStartAndEndDate();

            if (tableDateRange != null) // TODO default value
            {
                TimeTableDateRange     = $"{tableDateRange.Item1.Date.ToLongDateString()} - {tableDateRange.Item2.Date.ToLongDateString()}";
                EarliestDateTimeInData = tableDateRange.Item1;
                LatestDateTimeInData   = tableDateRange.Item2;
            }

            /*TimeZoneInfo clientTimeZone = clientSession.DeviceTimeZone; TODO unused
             * DateTimeOffset currentDateTimeUtc = DateTimeOffset.UtcNow;
             * DateTimeOffset clientDateTimeAtDayStart;
             * if (clientTimeZone.Id != "UTC")
             * {
             *  DateTimeOffset currentClientTime = TimeZoneInfo.ConvertTime(currentDateTimeUtc, clientTimeZone);
             *  DateTime clientDateTimeAtDayStartAsDateTimeLocal = new DateTime(currentClientTime.Year, currentClientTime.Month, currentClientTime.Day, 0, 0, 0, 1, DateTimeKind.Unspecified);
             *  DateTime clientUtcAtDayStartAsDateTimeUtc = TimeZoneInfo.ConvertTime(clientDateTimeAtDayStartAsDateTimeLocal, clientTimeZone, TimeZoneInfo.Utc);
             *  TimeSpan clientOffsetAtDayStart = clientTimeZone.GetUtcOffset(clientUtcAtDayStartAsDateTimeUtc);
             *  clientDateTimeAtDayStart = new DateTimeOffset(currentClientTime.Year, currentClientTime.Month, currentClientTime.Day, 0, 0, 0, 1, clientOffsetAtDayStart);
             * }
             * else
             * {
             *  clientDateTimeAtDayStart = new DateTimeOffset(currentDateTimeUtc.Year, currentDateTimeUtc.Month, currentDateTimeUtc.Day, 0, 0, 0, 1, TimeSpan.Zero);
             * }*/
            // ReferenceForTimeline = clientDateTimeAtDayStart;
        }
        public static void UpdateTrackerOptionsCookie(this TrackerClientSession currentSession, HttpResponse response, string cookieName)
        {
            var updatedCookieValue = MakeCookieValue(currentSession);

            if (!string.IsNullOrEmpty(updatedCookieValue))
            {
                response.Cookies.Append(cookieName, updatedCookieValue,
                                        new CookieOptions
                {
                    Expires  = DateTimeOffset.UtcNow.AddYears(1),
                    SameSite = SameSiteMode.Strict,
                    HttpOnly = false     // TODO security documentation
                });
            }
            else
            {
                response.Cookies.Delete(cookieName);
            }
        }
 public static string MakeCookieValue(this TrackerClientSession currentSession)
 => JsonConvert.SerializeObject(currentSession, new JsonSerializerSettings()
 {
     NullValueHandling = NullValueHandling.Ignore
 });
 public static TimeTable TranformTrackedTimesForView(this TimeTable timeTable, TrackerClientSession clientSession)
 {
     foreach (var workUnit in timeTable.WorkUnits)
     {
         TimeZoneInfo targetTimeZone = null;
         if (workUnit.IsDisplayAgendaTimeZone)
         {
             targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(workUnit.TimeZoneIdAgenda);
         }
         else if (!clientSession.IsDisplayAsTracked)
         {
             targetTimeZone = clientSession.ViewTimeZone;
         }
         foreach (var timeNorm in workUnit.TimeNorms)
         {
             if (timeNorm.StartTime != null)
             {
                 timeNorm.StartTime = timeNorm.StartTime.SetTrackedTimeForView(targetTimeZone);
             }
             if (timeNorm.EndTime != null)
             {
                 timeNorm.EndTime = timeNorm.EndTime.SetTrackedTimeForView(targetTimeZone);
             }
         }
     }
     return(timeTable);
 }
        /*public static TrackerService GetTestSession(this IStartup startup)
         * {
         *  startup.
         *  var session = new TrackerService()
         *  {
         *      Name = "Test Session 1",
         *      DateCreated = new DateTime(2016, 8, 1)
         *  };
         *  var idea = new Idea()
         *  {
         *      DateCreated = new DateTime(2016, 8, 1),
         *      Description = "Totally awesome idea",
         *      Name = "Awesome idea"
         *  };
         *  session.AddIdea(idea);
         *  return session;
         * }*/

        /*public static ITrackerServiceBuilder AddEntityFrameworkDatabases(this ITrackerServiceBuilder builder, TrackerContext context)
         * {
         *  if (builder == null)
         *  {
         *      throw new ArgumentNullException(nameof(builder));
         *  }
         *
         *  var services = builder.Services;
         *
         *  services.AddScoped<TrackerContext, TrackerContext>();
         *
         *  return builder;
         * }*/

        public static TimeStamp SetTrackedTimeForView(this TimeStamp timeStamp, TrackerClientSession clientSession)
        => SetTrackedTimeForView(timeStamp, clientSession.ViewTimeZone);