0

I have a request approval sequential workflow that has many tasks created upon approval.

For each approval, Im using a custom webpart that shows different panels based on the status of the request.

Each panels contains different types of user controls and so forth.

My Issue is I need to check whether the current logged in user has access to (assigned to) the latest workflow task (There are 4 levels of approval). If not I want to show an warning message.

Tip: All the users use the same url to the custom webpart page for approval (the onload event of the webpart checks whether the logged in user and the current active task)

0

Found the solution... quite simple actually.

This is what I entered in the page load method that returns a boolean indicating the authorization!

private bool IsLoggedInUserAuthorized(string loggedInUserName, int loggedInUserId)
    {
        bool isAuthorized = false;

        SPSecurity.RunWithElevatedPrivileges(delegate() 
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb("/IT"))
                {
                     // Add the logic to check whether task assigned to the task is same as the logged in user. Keep in mind not to get the logged in user within the elevated privileges method. If you do so you will get the admin account (sysadmin) 

                // Get the GUID of the request
                string paramRequestGuid = this.Page.Request.Params[VisualWebPart1.RequestIdParameterName];

                web.AllowUnsafeUpdates = true;

                SPList tasksList = web.Lists[ListName.Tasks];
                SPList list = web.Lists[ListName.ITChangeRequest];

                SPListItem requestItem = list.Items[Guid.Parse(paramRequestGuid)];

                if (requestItem != null)
                {
                    SPWorkflowTaskCollection collection = requestItem.Workflows[0].Tasks;

                    if (collection.Count > 0)
                    {
                        SPWorkflowTask workflowTask = collection[0];

                        SPFieldUserValue userValue =
                            (SPFieldUserValue)workflowTask.Fields[SPBuiltInFieldId.AssignedTo].GetFieldValue(
                            workflowTask[SPBuiltInFieldId.AssignedTo].ToString());

                        if (userValue.User != null)
                        {
                            // Its a user assignment
                            if (userValue.User.LoginName.Equals(loggedInUserName)){ isAuthorized = true; }
                        }
                        else
                        {
                            try
                            {
                                SPUser currentUser = web.Groups.GetByID(userValue.LookupId).Users.GetByID(loggedInUserId);
                                isAuthorized = true;
                            }
                            catch (Exception ex)
                            {
                                web.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                }
                web.AllowUnsafeUpdates = false;

                }
            }
        });

        return isAuthorized; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.