public User updateUser(User updatedUser) { UPSBrowserLogger.LogDebug(loggingCategory, "updateUser invoked"); UPSBrowserLogger.LogDebug(loggingCategory, $"updatedUser.UserGuid: {updatedUser.UserGuid}"); User userToUpdate = _users.SingleOrDefault(user => user.UserGuid == updatedUser.UserGuid); if (userToUpdate == null) { UPSBrowserLogger.LogError(loggingCategory, $"User profile not found"); //UPSBrowserLogger.LogActivity(userToUpdate.Username, UPSBrowserLogger.LogActivityActionEnum.Update, UPSBrowserLogger.LogActivityResultEnum.Error); ActivityLogger.LogActivity(userToUpdate.AccountName, LogActivityActionEnum.Update, LogActivityResultEnum.Error); return(null); } //_users.Where() UPSBrowserLogger.LogError(loggingCategory, $"User profile found, updating properties"); userToUpdate.WorkEmail = updatedUser.WorkEmail; userToUpdate.AccountName = updatedUser.AccountName; userToUpdate.DisplayName = updatedUser.DisplayName; userToUpdate.JobTitle = updatedUser.JobTitle; userToUpdate.Department = updatedUser.Department; // log completed activity //UPSBrowserLogger.LogActivity(userToUpdate.Username, UPSBrowserLogger.LogActivityActionEnum.Update, UPSBrowserLogger.LogActivityResultEnum.Success); ActivityLogger.LogActivity(userToUpdate.AccountName, LogActivityActionEnum.Update, LogActivityResultEnum.Success); return(userToUpdate); }
public User createUser(User newUser, string identityProviderName) { UPSBrowserLogger.LogDebug(loggingCategory, "createUser invoked"); UPSBrowserLogger.LogDebug(loggingCategory, $"newUser.UserGuid: {newUser.UserGuid}"); newUser.UserGuid = Guid.NewGuid().ToString(); _users.Add(newUser); // log completed activity //UPSBrowserLogger.LogActivity(newUser.Username, UPSBrowserLogger.LogActivityActionEnum.Create, UPSBrowserLogger.LogActivityResultEnum.Success); ActivityLogger.LogActivity(newUser.AccountName, LogActivityActionEnum.Create, LogActivityResultEnum.Success); return(newUser); }
public bool deleteUserByGuid(string guid) { UPSBrowserLogger.LogDebug(loggingCategory, "deleteUserByGuid invoked"); UPSBrowserLogger.LogDebug(loggingCategory, $"guid: {guid}"); User userToDelete = _users.SingleOrDefault(user => user.UserGuid == guid); if (userToDelete == null) { UPSBrowserLogger.LogError(loggingCategory, $"User profile not found"); //UPSBrowserLogger.LogActivity(userToDelete.Username, UPSBrowserLogger.LogActivityActionEnum.Delete, UPSBrowserLogger.LogActivityResultEnum.Error); ActivityLogger.LogActivity(userToDelete.AccountName, LogActivityActionEnum.Delete, LogActivityResultEnum.Error); return(false); } UPSBrowserLogger.LogDebug(loggingCategory, $"userToDelete.AccountName: {userToDelete.AccountName}, userToDelete.WorkEmail: {userToDelete.WorkEmail}"); _users.Remove(userToDelete); UPSBrowserLogger.LogDebug(loggingCategory, "User profile deleted"); //UPSBrowserLogger.LogActivity(userToDelete.Username, UPSBrowserLogger.LogActivityActionEnum.Delete, UPSBrowserLogger.LogActivityResultEnum.Success); ActivityLogger.LogActivity(userToDelete.AccountName, LogActivityActionEnum.Delete, LogActivityResultEnum.Success); return(true); }
public bool deleteUserByGuid(string guid) { UPSBrowserLogger.LogDebug(loggingCategory, "deleteUserByGuid invoked"); UPSBrowserLogger.LogDebug(loggingCategory, $"guid: {guid}"); bool result = false; string accountNameForLogger = guid; try { SPSecurity.RunWithElevatedPrivileges(delegate() { UPSBrowserLogger.LogDebug(loggingCategory, "Running with elevated privileges"); // Save the original HttpContext and set it to null // solution to enable impersonated access to UPS from here: // https://weblogs.asp.net/sreejukg/access-denied-error-when-retrieving-user-profiles-count-from-sharepoint HttpContext savedHttpContext = HttpContext.Current; HttpContext.Current = null; // Access the User Profile Service try { SPServiceContext serviceContext = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default); UPSBrowserLogger.LogDebug(loggingCategory, "Reference to SPServiceContext obtained"); UserProfileManager userProfileManager = new UserProfileManager(serviceContext); UPSBrowserLogger.LogDebug(loggingCategory, "Reference to UserProfileManager obtained"); UserProfile userProfile = userProfileManager.GetUserProfile(new Guid(guid)); if (userProfile == null) { UPSBrowserLogger.LogError(loggingCategory, $"User profile with guid {guid} not found in User Profile Service"); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Delete, LogActivityResultEnum.Error); return; //exit delegate block } ; UPSBrowserLogger.LogDebug(loggingCategory, $"userProfile.AccountName: {userProfile.AccountName}, userProfile.DisplayName: {userProfile.DisplayName}"); accountNameForLogger = userProfile.AccountName; userProfileManager.RemoveUserProfile(new Guid(guid)); string outputString = $"User profile with guid {guid} deleted"; UPSBrowserLogger.LogDebug(loggingCategory, outputString); result = true; } catch (System.Exception e) { UPSBrowserLogger.LogError(loggingCategory, e.Message); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Delete, LogActivityResultEnum.Error); } finally { // Restore HttpContext HttpContext.Current = savedHttpContext; }; }); } catch (System.Exception e) { UPSBrowserLogger.LogError(loggingCategory, $"Error while trying to elevate privileges: {e.Message}"); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Delete, LogActivityResultEnum.Error); }; if (result) { ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Delete, LogActivityResultEnum.Success); } ; return(result); }
public User createUser(User newUser, string identityProviderName) { UPSBrowserLogger.LogDebug(loggingCategory, "createUser invoked"); UPSBrowserLogger.LogDebug(loggingCategory, $"newUser.AccountName: {newUser.AccountName}, newUser.WorkEmail: {newUser.WorkEmail}, newUser.DisplayName: {newUser.DisplayName}"); UPSBrowserLogger.LogDebug(loggingCategory, $"identityProviderName: {identityProviderName}"); User userToReturn = null; string accountNameForLogger = newUser.WorkEmail; try { SPSecurity.RunWithElevatedPrivileges(delegate() { UPSBrowserLogger.LogDebug(loggingCategory, "Running with elevated privileges"); // Save the original HttpContext and set it to null // solution to enable impersonated access to UPS from here: // https://weblogs.asp.net/sreejukg/access-denied-error-when-retrieving-user-profiles-count-from-sharepoint HttpContext savedHttpContext = HttpContext.Current; HttpContext.Current = null; // Access the User Profile Service try { SPServiceContext serviceContext = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default); UPSBrowserLogger.LogDebug(loggingCategory, "Reference to SPServiceContext obtained"); UserProfileManager userProfileManager = new UserProfileManager(serviceContext); UPSBrowserLogger.LogDebug(loggingCategory, "Reference to UserProfileManager obtained"); string accountName = identityProvidersHelper.getAccountNameForEmail(newUser.WorkEmail, identityProviderName); accountNameForLogger = accountName; UserProfile userProfile = userProfileManager.CreateUserProfile(accountName, newUser.DisplayName); if (userProfile == null) { UPSBrowserLogger.LogError(loggingCategory, $"Failed to create user profile with AccountName {accountName}"); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Create, LogActivityResultEnum.Error); return; //exit delegate block } ; userProfile[PropertyConstants.WorkEmail].Value = newUser.WorkEmail; userProfile[PropertyConstants.FirstName].Value = newUser.FirstName; userProfile[PropertyConstants.LastName].Value = newUser.LastName; userProfile[PropertyConstants.Department].Value = newUser.Department; userProfile[PropertyConstants.JobTitle].Value = newUser.JobTitle; userProfile[PropertyConstants.Title].Value = newUser.JobTitle; // Title is synced from UPS to User Information List! userProfile[PropertyConstants.WorkPhone].Value = newUser.WorkPhone; userProfile[PropertyConstants.CellPhone].Value = newUser.CellPhone; userProfile.Commit(); UPSBrowserLogger.LogDebug(loggingCategory, $"userProfile.AccountName: {userProfile.AccountName}, userProfile.DisplayName: {userProfile.DisplayName}, userProfile.AccountName: {userProfile[PropertyConstants.UserGuid]}"); userToReturn = UserProfileToUser(userProfile); string outputString = $"Retrieved user properties - Email: {userToReturn.WorkEmail}, AccountName: {userToReturn.AccountName}, DisplayName: {userToReturn.DisplayName}, UserGuid: {userToReturn.UserGuid}, Department: {userToReturn.Department}, JobTitle: {userToReturn.JobTitle}"; UPSBrowserLogger.LogDebug(loggingCategory, outputString); } catch (System.Exception e) { UPSBrowserLogger.LogError(loggingCategory, e.Message); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Create, LogActivityResultEnum.Error); } finally { // Restore HttpContext HttpContext.Current = savedHttpContext; }; }); } catch (System.Exception e) { UPSBrowserLogger.LogError(loggingCategory, $"Error while trying to elevate privileges: {e.Message}"); ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Create, LogActivityResultEnum.Error); }; if (userToReturn != null) { ActivityLogger.LogActivity(accountNameForLogger, LogActivityActionEnum.Create, LogActivityResultEnum.Success); } ; return(userToReturn); }