private EventCalendar ResolveCalendarSettingOrThrow(RockContext rockContext, string calendarSettingValue) { var calendarService = new EventCalendarService(rockContext); EventCalendar calendar = null; // Verify that a calendar reference has been provided. if (string.IsNullOrWhiteSpace(calendarSettingValue)) { throw new Exception($"A calendar reference must be specified."); } // Get by ID. var calendarId = calendarSettingValue.AsIntegerOrNull(); if (calendarId != null) { calendar = calendarService.Get(calendarId.Value); } // Get by Guid. if (calendar == null) { var calendarGuid = calendarSettingValue.AsGuidOrNull(); if (calendarGuid != null) { calendar = calendarService.Get(calendarGuid.Value); } } // Get By Name. if (calendar == null) { var calendarName = calendarSettingValue.ToString(); if (!string.IsNullOrWhiteSpace(calendarName)) { calendar = calendarService.Queryable() .Where(x => x.Name != null && x.Name.Equals(calendarName, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); } } if (calendar == null) { throw new Exception($"Cannot find a calendar matching the reference \"{ calendarSettingValue }\"."); } return(calendar); }
/// <summary> /// Ensure the current user is authorized to view the calendar. If all are allowed then current user is not evaluated. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> private bool ValidateSecurity(HttpContext context) { int calendarId; if (request.QueryString["calendarid"] == null || !int.TryParse(request.QueryString["calendarId"], out calendarId)) { SendNotAuthorized(context); return(false); } RockContext rockContext = new RockContext(); EventCalendarService eventCalendarService = new EventCalendarService(rockContext); EventCalendar eventCalendar = eventCalendarService.Get(calendarId); if (eventCalendar == null) { SendBadRequest(context); return(false); } // Need to replace CurrentUser with the result of a person token, in the meantime this will always create a null person unless directly downloadng the ical when logged into the site UserLogin currentUser = new UserLoginService(rockContext).GetByUserName(UserLogin.GetCurrentUserName()); Person currentPerson = currentUser != null ? currentUser.Person : null; var isAuthorized = eventCalendar.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson); if (isAuthorized) { return(true); } SendNotAuthorized(context); return(false); }
/// <summary> /// Handles the Click event of the btnDelete control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnDelete_Click(object sender, EventArgs e) { using (var rockContext = new RockContext()) { EventCalendarService eventCalendarService = new EventCalendarService(rockContext); AuthService authService = new AuthService(rockContext); EventCalendar eventCalendar = eventCalendarService.Get(int.Parse(hfEventCalendarId.Value)); if (eventCalendar != null) { bool adminAllowed = UserCanAdministrate || eventCalendar.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson); if (!adminAllowed) { mdDeleteWarning.Show("You are not authorized to delete this calendar.", ModalAlertType.Information); return; } string errorMessage; if (!eventCalendarService.CanDelete(eventCalendar, out errorMessage)) { mdDeleteWarning.Show(errorMessage, ModalAlertType.Information); return; } eventCalendarService.Delete(eventCalendar); rockContext.SaveChanges(); } } NavigateToParentPage(); }
/// <summary> /// Ensure the current user is authorized to view the calendar. If all are allowed then current user is not evaluated. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> private bool ValidateSecurity(HttpContext context) { int calendarId; if (request.QueryString["calendarid"] == null || !int.TryParse(request.QueryString["calendarId"], out calendarId)) { SendNotAuthorized(context); return(false); } RockContext rockContext = new RockContext(); EventCalendarService eventCalendarService = new EventCalendarService(rockContext); EventCalendar eventCalendar = eventCalendarService.Get(calendarId); if (eventCalendar == null) { SendBadRequest(context); return(false); } // If this is a public calendar then just return true if (eventCalendar.IsAllowedByDefault("View")) { return(true); } UserLogin currentUser = new UserLoginService(rockContext).GetByUserName(UserLogin.GetCurrentUserName()); Person currentPerson = currentUser != null ? currentUser.Person : null; if (currentPerson != null && eventCalendar.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson)) { return(true); } SendNotAuthorized(context); return(false); }
/// <summary> /// Shows the item attributes. /// </summary> private void ShowItemAttributes() { var eventCalendarList = new List <int> { (_calendarId ?? 0) }; eventCalendarList.AddRange(cblCalendars.SelectedValuesAsInt); wpAttributes.Visible = false; phAttributes.Controls.Clear(); using (var rockContext = new RockContext()) { var eventCalendarService = new EventCalendarService(rockContext); foreach (int eventCalendarId in eventCalendarList.Distinct()) { EventCalendarItem eventCalendarItem = ItemsState.FirstOrDefault(i => i.EventCalendarId == eventCalendarId); if (eventCalendarItem == null) { eventCalendarItem = new EventCalendarItem(); eventCalendarItem.EventCalendarId = eventCalendarId; ItemsState.Add(eventCalendarItem); } eventCalendarItem.LoadAttributes(); if (eventCalendarItem.Attributes.Count > 0) { wpAttributes.Visible = true; phAttributes.Controls.Add(new LiteralControl(String.Format("<h3>{0}</h3>", eventCalendarService.Get(eventCalendarId).Name))); PlaceHolder phcalAttributes = new PlaceHolder(); Rock.Attribute.Helper.AddEditControls(eventCalendarItem, phAttributes, true, BlockValidationGroup); } } } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click( object sender, EventArgs e ) { EventCalendar eventCalendar; using ( var rockContext = new RockContext() ) { EventCalendarService eventCalendarService = new EventCalendarService( rockContext ); EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService( rockContext ); ContentChannelService contentChannelService = new ContentChannelService( rockContext ); AttributeService attributeService = new AttributeService( rockContext ); AttributeQualifierService qualifierService = new AttributeQualifierService( rockContext ); int eventCalendarId = int.Parse( hfEventCalendarId.Value ); if ( eventCalendarId == 0 ) { eventCalendar = new EventCalendar(); eventCalendarService.Add( eventCalendar ); } else { eventCalendar = eventCalendarService.Get( eventCalendarId ); } eventCalendar.IsActive = cbActive.Checked; eventCalendar.Name = tbName.Text; eventCalendar.Description = tbDescription.Text; eventCalendar.IconCssClass = tbIconCssClass.Text; if ( !eventCalendar.IsValid ) { // Controls will render the error messages return; } // need WrapTransaction due to Attribute saves rockContext.WrapTransaction( () => { rockContext.SaveChanges(); var dbChannelGuids = eventCalendarContentChannelService.Queryable() .Where( c => c.EventCalendarId == eventCalendar.Id ) .Select( c => c.Guid ) .ToList(); var uiChannelGuids = ContentChannelsState.Select( c => c.Key ).ToList(); var toDelete = eventCalendarContentChannelService .Queryable() .Where( c => dbChannelGuids.Contains( c.Guid ) && !uiChannelGuids.Contains( c.Guid )); eventCalendarContentChannelService.DeleteRange( toDelete ); contentChannelService.Queryable() .Where( c => uiChannelGuids.Contains( c.Guid ) && !dbChannelGuids.Contains( c.Guid ) ) .ToList() .ForEach( c => { var eventCalendarContentChannel = new EventCalendarContentChannel(); eventCalendarContentChannel.EventCalendarId = eventCalendar.Id; eventCalendarContentChannel.ContentChannelId = c.Id; eventCalendarContentChannelService.Add( eventCalendarContentChannel ); } ); rockContext.SaveChanges(); /* Save Attributes */ string qualifierValue = eventCalendar.Id.ToString(); SaveAttributes( new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, AttributesState, rockContext ); // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar eventCalendar = eventCalendarService.Get( eventCalendar.Id ); if ( eventCalendar != null ) { if ( !eventCalendar.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.VIEW, CurrentPerson, rockContext ); } if ( !eventCalendar.IsAuthorized( Authorization.EDIT, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.EDIT, CurrentPerson, rockContext ); } if ( !eventCalendar.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.ADMINISTRATE, CurrentPerson, rockContext ); } } } ); } // Redirect back to same page so that item grid will show any attributes that were selected to show on grid var qryParams = new Dictionary<string, string>(); qryParams["EventCalendarId"] = eventCalendar.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Handles the Click event of the btnDelete control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnDelete_Click( object sender, EventArgs e ) { using ( var rockContext = new RockContext() ) { EventCalendarService eventCalendarService = new EventCalendarService( rockContext ); AuthService authService = new AuthService( rockContext ); EventCalendar eventCalendar = eventCalendarService.Get( int.Parse( hfEventCalendarId.Value ) ); if ( eventCalendar != null ) { bool adminAllowed = UserCanAdministrate || eventCalendar.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ); if ( !adminAllowed ) { mdDeleteWarning.Show( "You are not authorized to delete this calendar.", ModalAlertType.Information ); return; } string errorMessage; if ( !eventCalendarService.CanDelete( eventCalendar, out errorMessage ) ) { mdDeleteWarning.Show( errorMessage, ModalAlertType.Information ); return; } eventCalendarService.Delete( eventCalendar ); rockContext.SaveChanges(); } } NavigateToParentPage(); }
/// <summary> /// Shows the item attributes. /// </summary> private void ShowItemAttributes() { var eventCalendarList = new List<int> { ( _calendarId ?? 0 ) }; eventCalendarList.AddRange( cblCalendars.SelectedValuesAsInt ); wpAttributes.Visible = false; phAttributes.Controls.Clear(); using ( var rockContext = new RockContext() ) { var eventCalendarService = new EventCalendarService( rockContext ); foreach ( int eventCalendarId in eventCalendarList.Distinct() ) { EventCalendarItem eventCalendarItem = ItemsState.FirstOrDefault( i => i.EventCalendarId == eventCalendarId ); if ( eventCalendarItem == null ) { eventCalendarItem = new EventCalendarItem(); eventCalendarItem.EventCalendarId = eventCalendarId; ItemsState.Add( eventCalendarItem ); } eventCalendarItem.LoadAttributes(); if ( eventCalendarItem.Attributes.Count > 0 ) { wpAttributes.Visible = true; phAttributes.Controls.Add( new LiteralControl( String.Format( "<h3>{0}</h3>", eventCalendarService.Get( eventCalendarId ).Name ) ) ); PlaceHolder phcalAttributes = new PlaceHolder(); Rock.Attribute.Helper.AddEditControls( eventCalendarItem, phAttributes, true, BlockValidationGroup ); } } } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { EventCalendar eventCalendar; using (var rockContext = new RockContext()) { EventCalendarService eventCalendarService = new EventCalendarService(rockContext); EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService(rockContext); ContentChannelService contentChannelService = new ContentChannelService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService qualifierService = new AttributeQualifierService(rockContext); int eventCalendarId = int.Parse(hfEventCalendarId.Value); if (eventCalendarId == 0) { eventCalendar = new EventCalendar(); eventCalendarService.Add(eventCalendar); } else { eventCalendar = eventCalendarService.Get(eventCalendarId); } eventCalendar.IsActive = cbActive.Checked; eventCalendar.Name = tbName.Text; eventCalendar.Description = tbDescription.Text; eventCalendar.IconCssClass = tbIconCssClass.Text; eventCalendar.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAttributes, eventCalendar); if (!eventCalendar.IsValid) { // Controls will render the error messages return; } // need WrapTransaction due to Attribute saves rockContext.WrapTransaction(() => { rockContext.SaveChanges(); eventCalendar.SaveAttributeValues(rockContext); var dbChannelGuids = eventCalendarContentChannelService.Queryable() .Where(c => c.EventCalendarId == eventCalendar.Id) .Select(c => c.Guid) .ToList(); var uiChannelGuids = ContentChannelsState.Select(c => c.Key).ToList(); var toDelete = eventCalendarContentChannelService .Queryable() .Where(c => dbChannelGuids.Contains(c.Guid) && !uiChannelGuids.Contains(c.Guid)); eventCalendarContentChannelService.DeleteRange(toDelete); contentChannelService.Queryable() .Where(c => uiChannelGuids.Contains(c.Guid) && !dbChannelGuids.Contains(c.Guid)) .ToList() .ForEach(c => { var eventCalendarContentChannel = new EventCalendarContentChannel(); eventCalendarContentChannel.EventCalendarId = eventCalendar.Id; eventCalendarContentChannel.ContentChannelId = c.Id; eventCalendarContentChannelService.Add(eventCalendarContentChannel); }); rockContext.SaveChanges(); /* Save Event Attributes */ string qualifierValue = eventCalendar.Id.ToString(); SaveAttributes(new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, EventAttributesState, rockContext); // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar eventCalendar = eventCalendarService.Get(eventCalendar.Id); if (eventCalendar != null) { if (!eventCalendar.IsAuthorized(Authorization.VIEW, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.VIEW, CurrentPerson, rockContext); } if (!eventCalendar.IsAuthorized(Authorization.EDIT, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.EDIT, CurrentPerson, rockContext); } if (!eventCalendar.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.ADMINISTRATE, CurrentPerson, rockContext); } } }); } // Redirect back to same page so that item grid will show any attributes that were selected to show on grid var qryParams = new Dictionary <string, string>(); qryParams["EventCalendarId"] = eventCalendar.Id.ToString(); NavigateToPage(RockPage.Guid, qryParams); }