public async Task <IActionResult> ExternalCalendarProviders() { var user = await _userManager.GetCurrentUserAsync(); if (!user.IsSuccess) { return(NotFound()); } var userId = user.Result.Id; var factory = new ExternalCalendarProviderFactory(); var providers = factory.GetProvidersInfo(); var exportModel = new List <ExternalCalendarProviderSettingsViewModel>(); foreach (var provider in providers) { var providerConf = new ExternalCalendarProviderSettingsViewModel { ProviderName = provider.ProviderName, FontAwesomeIcon = provider.FontAwesomeIcon, DisplayName = provider.DisplayName }; var isEnabledRequest = await _userSettingsService.IsProviderEnabledAsync(userId, provider.ProviderName); providerConf.IsEnabled = isEnabledRequest.IsSuccess; if (!isEnabledRequest.IsSuccess) { exportModel.Add(providerConf); continue; } var providerService = factory.CreateService(provider.ProviderName); var authResult = await providerService.AuthorizeAsync(userId); if (authResult.IsSuccess) { var provideruserRequest = await providerService.GetUserAsync(); if (provideruserRequest.IsSuccess) { var providerUser = provideruserRequest.Result; providerConf.User = new ExternalCalendarUser { IsAuthorized = true, UserName = providerUser.DisplayName, EmailAdress = providerUser.EmailAddress, ImageUrl = providerUser.ImageUrl }; } } exportModel.Add(providerConf); } return(View(exportModel)); }
public async Task <IActionResult> CalendarExternalLogin(string provider, string returnUrl = null) { var user = await _userManager.GetCurrentUserAsync(); if (!user.IsSuccess) { return(NotFound()); } var factory = new ExternalCalendarProviderFactory(); IActionResult result; switch (provider) { case "GoogleCalendarProvider": { if (factory.GetProviders().Contains(provider)) { var serviceProvider = factory.CreateService(provider); await serviceProvider.AuthorizeAsync(user.Result.Id, true); result = RedirectToAction("ExternalCalendarProviders"); } else { result = RedirectToAction(nameof(ExternalCalendarProviders)); } } break; case "OutlookCalendarProvider": { var redirectUrl = Url.Action(nameof(OutlookCalendarExternalLoginCallback), "Calendar", new { returnUrl, gearUserId = user.Result.Id, provider }); const string instanceProvider = "Microsoft"; var properties = _signInManager.ConfigureExternalAuthenticationProperties(instanceProvider, redirectUrl); result = Challenge(properties, instanceProvider); } break; default: { result = RedirectToAction(nameof(ExternalCalendarProviders)); } break; } return(result); }
/// <summary> /// Register events /// </summary> /// <param name="serviceCollection"></param> /// <returns></returns> public static CalendarServiceCollection RegisterSyncOnExternalCalendars(this CalendarServiceCollection serviceCollection) { //On event created CalendarEvents.SystemCalendarEvents.OnEventCreated += async(sender, args) => { var calendarManager = IoC.Resolve <ICalendarManager>(); var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>(); var evtRequest = await calendarManager.GetEventByIdAsync(args.EventId); if (!evtRequest.IsSuccess) { return; } var evt = evtRequest.Result; var factory = new ExternalCalendarProviderFactory(); var providers = factory.GetProviders(); foreach (var provider in providers) { var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider); if (!isProviderEnabledForUser.IsSuccess) { continue; } var providerService = factory.CreateService(provider); var authRequest = await providerService.AuthorizeAsync(evt.Organizer); if (!authRequest.IsSuccess) { continue; } var syncResult = await providerService.PushEventAsync(EventMapper.Map(evt)); if (!syncResult.IsSuccess) { Debug.WriteLine(syncResult.Errors); } } await calendarManager.SetEventSyncState(evt.Id, true); }; //On event update CalendarEvents.SystemCalendarEvents.OnEventUpdated += async(sender, args) => { var calendarManager = IoC.Resolve <ICalendarManager>(); var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>(); var evtRequest = await calendarManager.GetEventByIdAsync(args.EventId); if (!evtRequest.IsSuccess) { return; } var evt = evtRequest.Result; var factory = new ExternalCalendarProviderFactory(); var providers = factory.GetProviders(); foreach (var provider in providers) { var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider); if (!isProviderEnabledForUser.IsSuccess) { continue; } var providerService = factory.CreateService(provider); var authRequest = await providerService.AuthorizeAsync(evt.Organizer); if (!authRequest.IsSuccess) { continue; } if (!evt.Synced) { await providerService.PushEventAsync(EventMapper.Map(evt)); } else { var attrRequest = await userSettingsService.GetEventAttributeAsync(evt.Id, $"{provider}_evtId"); if (attrRequest.IsSuccess) { var providerEventId = attrRequest.Result; var syncResult = await providerService.UpdateEventAsync(EventMapper.Map(evt), providerEventId); if (!syncResult.IsSuccess) { Debug.WriteLine(syncResult.Errors); } } else { await providerService.PushEventAsync(EventMapper.Map(evt)); } } } if (!evt.Synced) { await calendarManager.SetEventSyncState(evt.Id, true); } }; //On delete Event CalendarEvents.SystemCalendarEvents.OnEventDeleted += async(sender, args) => { var calendarManager = IoC.Resolve <ICalendarManager>(); var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>(); var evtRequest = await calendarManager.GetEventByIdAsync(args.EventId); if (!evtRequest.IsSuccess) { return; } var evt = evtRequest.Result; if (!evt.Synced) { return; } var factory = new ExternalCalendarProviderFactory(); var providers = factory.GetProviders(); foreach (var provider in providers) { var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider); if (!isProviderEnabledForUser.IsSuccess) { continue; } var providerService = factory.CreateService(provider); var authRequest = await providerService.AuthorizeAsync(evt.Organizer); if (!authRequest.IsSuccess) { continue; } var attrRequest = await userSettingsService.GetEventAttributeAsync(evt.Id, $"{provider}_evtId"); if (!attrRequest.IsSuccess) { continue; } var providerEventId = attrRequest.Result; await providerService.DeleteEventAsync(providerEventId); } }; return(serviceCollection); }