@@ -34,10 +34,13 @@ public class CalendarController : SecureBaseController
3434 private readonly IEventAggregator _eventAggregator ;
3535 private readonly IAuthorizationService _authorizationService ;
3636 private readonly IUserProfileService _userProfileService ;
37+ private readonly IPermissionsService _permissionsService ;
38+ private readonly IPersonnelRolesService _personnelRolesService ;
3739
3840 public CalendarController ( IDepartmentsService departmentsService , IUsersService usersService , ICalendarService calendarService ,
3941 IDepartmentGroupsService departmentGroupsService , IGeoLocationProvider geoLocationProvider , IEventAggregator eventAggregator ,
40- IAuthorizationService authorizationService , IUserProfileService userProfileService )
42+ IAuthorizationService authorizationService , IUserProfileService userProfileService ,
43+ IPermissionsService permissionsService , IPersonnelRolesService personnelRolesService )
4144 {
4245 _departmentsService = departmentsService ;
4346 _usersService = usersService ;
@@ -47,6 +50,8 @@ public CalendarController(IDepartmentsService departmentsService, IUsersService
4750 _eventAggregator = eventAggregator ;
4851 _authorizationService = authorizationService ;
4952 _userProfileService = userProfileService ;
53+ _permissionsService = permissionsService ;
54+ _personnelRolesService = personnelRolesService ;
5055 }
5156 #endregion Private Members and Constructors
5257
@@ -69,13 +74,25 @@ public async Task<IActionResult> Index()
6974 model . UpcomingItems = new List < CalendarItem > ( ) ;
7075 model . UpcomingItems = await _calendarService . GetUpcomingCalendarItemsAsync ( DepartmentId , DateTime . UtcNow ) ;
7176
77+ // Check calendar sync permission
78+ var calSyncPermission = await _permissionsService . GetPermissionByDepartmentTypeAsync ( DepartmentId , PermissionTypes . UseCalendarSync ) ;
79+ var department = model . Department ;
80+ var isAdmin = department . IsUserAnAdmin ( UserId ) ;
81+ var group = await _departmentGroupsService . GetGroupForUserAsync ( UserId , DepartmentId ) ;
82+ var isGroupAdmin = group != null && group . IsUserGroupAdmin ( UserId ) ;
83+ var roles = await _personnelRolesService . GetRolesForUserAsync ( UserId , DepartmentId ) ;
84+ model . CanUseCalendarSync = _permissionsService . IsUserAllowed ( calSyncPermission , isAdmin , isGroupAdmin , roles ) ;
85+
7286 // Populate calendar sync token for the subscribe panel.
73- var profile = await _userProfileService . GetProfileByUserIdAsync ( UserId ) ;
74- if ( profile != null && ! String . IsNullOrWhiteSpace ( profile . CalendarSyncToken ) )
87+ if ( model . CanUseCalendarSync )
7588 {
76- model . CalendarSyncToken = profile . CalendarSyncToken ;
77- var feedToken = await _calendarService . GetCalendarFeedTokenAsync ( DepartmentId , UserId ) ;
78- model . CalendarSubscriptionUrl = $ "{ SystemBehaviorConfig . ResgridApiBaseUrl } /api/v4/CalendarExport/CalendarFeed/{ feedToken } ";
89+ var profile = await _userProfileService . GetProfileByUserIdAsync ( UserId ) ;
90+ if ( profile != null && ! String . IsNullOrWhiteSpace ( profile . CalendarSyncToken ) )
91+ {
92+ model . CalendarSyncToken = profile . CalendarSyncToken ;
93+ var feedToken = await _calendarService . GetCalendarFeedTokenAsync ( DepartmentId , UserId ) ;
94+ model . CalendarSubscriptionUrl = $ "{ SystemBehaviorConfig . ResgridApiBaseUrl } /api/v4/CalendarExport/CalendarFeed/{ feedToken } ";
95+ }
7996 }
8097
8198 return View ( model ) ;
@@ -895,6 +912,13 @@ public async Task<IActionResult> GetMapDataForItem(int calendarItemId)
895912 [ ValidateAntiForgeryToken ]
896913 public async Task < IActionResult > ActivateCalendarSync ( CancellationToken cancellationToken )
897914 {
915+ var permission = await _permissionsService . GetPermissionByDepartmentTypeAsync ( DepartmentId , PermissionTypes . UseCalendarSync ) ;
916+ var dept = await _departmentsService . GetDepartmentByIdAsync ( DepartmentId , false ) ;
917+ var grp = await _departmentGroupsService . GetGroupForUserAsync ( UserId , DepartmentId ) ;
918+ var roles = await _personnelRolesService . GetRolesForUserAsync ( UserId , DepartmentId ) ;
919+ if ( ! _permissionsService . IsUserAllowed ( permission , dept . IsUserAnAdmin ( UserId ) , grp != null && grp . IsUserGroupAdmin ( UserId ) , roles ) )
920+ return Unauthorized ( ) ;
921+
898922 await _calendarService . ActivateCalendarSyncAsync ( DepartmentId , UserId , cancellationToken ) ;
899923 return RedirectToAction ( "Index" ) ;
900924 }
@@ -908,10 +932,41 @@ public async Task<IActionResult> ActivateCalendarSync(CancellationToken cancella
908932 [ ValidateAntiForgeryToken ]
909933 public async Task < IActionResult > RegenerateCalendarSync ( CancellationToken cancellationToken )
910934 {
935+ var permission = await _permissionsService . GetPermissionByDepartmentTypeAsync ( DepartmentId , PermissionTypes . UseCalendarSync ) ;
936+ var dept = await _departmentsService . GetDepartmentByIdAsync ( DepartmentId , false ) ;
937+ var grp = await _departmentGroupsService . GetGroupForUserAsync ( UserId , DepartmentId ) ;
938+ var roles = await _personnelRolesService . GetRolesForUserAsync ( UserId , DepartmentId ) ;
939+ if ( ! _permissionsService . IsUserAllowed ( permission , dept . IsUserAnAdmin ( UserId ) , grp != null && grp . IsUserGroupAdmin ( UserId ) , roles ) )
940+ return Unauthorized ( ) ;
941+
911942 await _calendarService . RegenerateCalendarSyncAsync ( DepartmentId , UserId , cancellationToken ) ;
912943 return RedirectToAction ( "Index" ) ;
913944 }
914945
946+ /// <summary>
947+ /// Admin action: regenerates calendar sync tokens for ALL users in the department who have one provisioned.
948+ /// </summary>
949+ [ HttpPost ]
950+ [ Authorize ( Policy = ResgridResources . Department_Update ) ]
951+ [ ValidateAntiForgeryToken ]
952+ public async Task < IActionResult > RegenerateAllCalendarSyncTokens ( CancellationToken cancellationToken )
953+ {
954+ var members = await _departmentsService . GetAllMembersForDepartmentAsync ( DepartmentId ) ;
955+ if ( members != null )
956+ {
957+ foreach ( var member in members )
958+ {
959+ var profile = await _userProfileService . GetProfileByUserIdAsync ( member . UserId ) ;
960+ if ( profile != null && ! string . IsNullOrWhiteSpace ( profile . CalendarSyncToken ) )
961+ {
962+ await _calendarService . RegenerateCalendarSyncAsync ( DepartmentId , member . UserId , cancellationToken ) ;
963+ }
964+ }
965+ }
966+
967+ return RedirectToAction ( "Index" ) ;
968+ }
969+
915970 // -- Check-In Attendance -------------------------------------------------------
916971
917972 [ HttpPost ]
0 commit comments