Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        private void InitLatestActivitiesListView()
        {
            UPSBrowserLogger.LogDebug(loggingCategory, "InitLatestActivitiesListView invoked");

            Tuple <string, string> ensureListResult = ActivityLogger.EnsureActivitiesList(); //it resturns a Tuple <listGuid, viewGuid>

            if (ensureListResult == null)
            {
                string errorMessage = "ActivityLogger.EnsureActivitiesList returned null";
                UPSBrowserLogger.LogError(loggingCategory, errorMessage);
                throw new Exception(errorMessage);
            }

            string listGuid = ensureListResult.Item1;
            string viewGuid = ensureListResult.Item2;

            UPSBrowserLogger.LogDebug(loggingCategory, $"listGuid: {listGuid}");
            UPSBrowserLogger.LogDebug(loggingCategory, $"viewGuid: {viewGuid}");

            Microsoft.SharePoint.WebPartPages.XsltListViewWebPart listViewWebPart = new Microsoft.SharePoint.WebPartPages.XsltListViewWebPart();
            listViewWebPart.ListId = new Guid(listGuid);

            listViewWebPart.Toolbar = "";

            string xmlDefinition = $@"
                <View Name=""{{{viewGuid}}}"" MobileView=""TRUE"" Type=""HTML"" Hidden=""TRUE"" DisplayName="""" Level=""1"" BaseViewID=""1"" ContentTypeID=""0x"" ImageUrl=""/_layouts/15/images/generic.png?rev=23"" >
                    <Query>
                        <OrderBy>
                            <FieldRef Name=""RegisteredDate"" Ascending=""FALSE""/>
                        </OrderBy>
                        <Where>
                            <Geq><FieldRef Name=""RegisteredDate""/><Value Type=""DateTime""><Today/></Value></Geq>
                        </Where>
                    </Query>
                    <ViewFields>
                        <FieldRef Name=""RegisteredDate""/>
                        <FieldRef Name=""Initiator""/>
                        <FieldRef Name=""User""/>
                        <FieldRef Name=""Action""/>
                        <FieldRef Name=""Result""/>
                        <FieldRef Name=""AdditionalInfo""/>
                    </ViewFields>
                    <RowLimit Paged=""TRUE"">30</RowLimit>
                    <Aggregations Value=""Off""/>
                    <JSLink>clienttemplates.js</JSLink>
                    <XslLink Default=""TRUE"">main.xsl</XslLink>
                    <Toolbar Type=""None""/>
                </View>
            ";

            listViewWebPart.XmlDefinition = xmlDefinition;

            listViewWebPart.AllowClose      = false;
            listViewWebPart.AllowConnect    = false;
            listViewWebPart.AllowEdit       = false;
            listViewWebPart.AllowHide       = false;
            listViewWebPart.AllowMinimize   = false;
            listViewWebPart.AllowZoneChange = false;
            listViewWebPart.ChromeType      = PartChromeType.None;
            PanelLatestActivities.Controls.Add(listViewWebPart);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }