/// <summary> /// Checks to see if the user running this program is an administrator in /// the Workflow Manager database. /// </summary> /// <returns>true if the user is an administrator; false otherwise</returns> protected bool CurrentUserIsWmxAdministrator() { bool retVal = false; string username = ESRI.ArcGIS.JTXUI.ConfigurationCache.GetCurrentSystemUser(ESRI.ArcGIS.JTXUI.ConfigurationCache.UseUserDomain); if (this.WmxDatabase != null) { IJTXUser3 user = this.WmxDatabase.ConfigurationManager.GetUser(username) as IJTXUser3; if (user != null) { retVal = user.IsAdministrator; } } return(retVal); }
/// <summary> /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed. /// </summary> /// <param name="paramValues"></param> /// <param name="trackCancel"></param> /// <param name="envMgr"></param> /// <param name="msgs"></param> public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs) { // Do some common error-checking base.Execute(paramValues, trackCancel, envMgr, msgs); try { // Ensure that the current user has admin access to the current Workflow Manager DB if (!CurrentUserIsWmxAdministrator()) { throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR); } // Stash away the executing user's information, if appropriate string username = ESRI.ArcGIS.JTXUI.ConfigurationCache.GetCurrentSystemUser(ESRI.ArcGIS.JTXUI.ConfigurationCache.UseUserDomain); IJTXConfiguration3 configMgr = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3; IJTXUser3 executingUser = configMgr.GetUser(username) as IJTXUser3; // Import the AD information string domain = System.Environment.UserDomainName; string domainUsername = string.Empty; string domainPassword = string.Empty; int numUsers = 0; int numGroups = 0; ActiveDirectoryHelper.SyncronizeJTXDatabaseWithActiveDirectory(this.WmxDatabase, domain, domainUsername, domainPassword, m_userGroup, m_groupGroup, out numGroups, out numUsers); // If the tool was set to preserve the current user's account and the user // was removed from the DB, then re-add their account if (configMgr.GetUser(username) == null) { if (m_preserveCurrentUser) { IJTXConfigurationEdit2 configEdit = this.WmxDatabase.ConfigurationManager as IJTXConfigurationEdit2; IJTXUserConfig newUser = configEdit.CreateUser() as IJTXUserConfig; newUser.FirstName_2 = executingUser.FirstName; newUser.FullName_2 = executingUser.FullName; newUser.LastName_2 = executingUser.LastName; newUser.UserName_2 = executingUser.UserName; (newUser as IJTXUser3).IsAdministrator = executingUser.IsAdministrator; newUser.Store(); msgs.AddMessage("User '" + username + "' not found in Active Directory group '" + m_userGroup + "'; re-added placeholder to Workflow Manager database"); } else { msgs.AddWarning("User '" + username + "' removed from Workflow Manager database"); } } // Update the output parameters WmauParameterMap paramMap = new WmauParameterMap(paramValues); IGPParameterEdit3 outParam = paramMap.GetParamEdit(C_PARAM_OUT_NUM_USERS); IGPLong value = new GPLongClass(); value.Value = numUsers; outParam.Value = value as IGPValue; outParam = paramMap.GetParamEdit(C_PARAM_OUT_NUM_GROUPS); value = new GPLongClass(); value.Value = numGroups; outParam.Value = value as IGPValue; msgs.AddMessage(Properties.Resources.MSG_DONE); } catch (WmauException wmEx) { try { msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message); } catch { // Catch anything else that possibly happens } } catch (Exception ex) { try { WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR); msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message); } catch { // Catch anything else that possibly happens } } finally { // Release any COM objects here! } }
/// <summary> /// Builds a domain containing the usernames of the users to whom a /// user can assign jobs. /// </summary> /// <param name="wmxDb">A reference to the active Workflow Manager database</param> /// <param name="username">The name of the user to be tested</param> /// <param name="extraValues">An array of string values to be added to the list</param> /// <returns>A coded value domain of strings</returns> public static IGPDomain BuildAssignableUsersDomain(IJTXDatabase3 wmxDb, string username, string[] extraValues) { IGPCodedValueDomain domain = null; // Only proceed if the user exists in the Workflow Manager database IJTXUser3 user = wmxDb.ConfigurationManager.GetUser(username) as IJTXUser3; if (user == null) { return(domain as IGPDomain); } // Case 1: If the user can assign the job to anyone, then // just use the "all users" list if (user.HasNamedPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_ASSIGN_ANY_JOB)) { domain = Common.WmauGpDomainBuilder.BuildUsersDomain(wmxDb, extraValues) as IGPCodedValueDomain; } else { domain = new GPCodedValueDomainClass(); string[] eligibleUsers = null; // Case 2: The user can assign jobs to anyone within any of their groups if (user.HasNamedPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_GROUP_JOB_ASSIGN)) { HashSet <string> usernames = new HashSet <string>(); IJTXUserGroupSet groups = user.Groups; for (int i = 0; i < groups.Count; i++) { IJTXUserGroup group = groups.get_Item(i); for (int j = 0; j < group.Users.Count; j++) { usernames.Add(group.Users.get_Item(j).UserName); } } eligibleUsers = usernames.ToArray(); } // Case 3: The user can assign jobs to themselves else if (user.HasNamedPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_INDIVIDUAL_JOB_ASSIGN)) { eligibleUsers = new string[] { username }; } // Case 4: The user can't assign jobs to anyone else { eligibleUsers = new string[0]; } // Sort the types first SortedList <string, string> sortedValues = new SortedList <string, string>(); for (int i = 0; i < eligibleUsers.Length; i++) { sortedValues.Add(eligibleUsers[i], null); } // Add the extra values, if any if (extraValues != null) { foreach (string s in extraValues) { sortedValues.Add(s, null); } } // Add the sorted types to the domain foreach (string value in sortedValues.Keys) { IGPValue tempGpVal = new GPStringClass(); tempGpVal.SetAsText(value); domain.AddCode(tempGpVal, value); } } return(domain as IGPDomain); }
/// <summary> /// Builds a domain consisting of the names of the system groups to which a /// user can assign a job. /// </summary> /// <param name="wmxDb">A reference to the active Workflow Manager database</param> /// <param name="username">The name of the user to be tested</param> /// <param name="extraValues">An array of string values to be added to the list</param> /// <returns>A coded value domain as an IGPDomain</returns> public static IGPDomain BuildAssignableGroupsDomain(IJTXDatabase3 wmxDb, string username, string[] extraValues) { IGPCodedValueDomain domain = new GPCodedValueDomainClass(); string[] eligibleGroups = null; // Only proceed if the user exists in the Workflow Manager database IJTXUser3 user = wmxDb.ConfigurationManager.GetUser(username) as IJTXUser3; if (user == null) { return(domain as IGPDomain); } // The groups to which this user can assign jobs are based on several // different permissions. Check these permissions, in order from least // restrictive to most restrictive. if (user.HasNamedPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_ASSIGN_ANY_JOB)) { int numGroups = wmxDb.ConfigurationManager.UserGroups.Count; eligibleGroups = new string[numGroups]; for (int i = 0; i < numGroups; i++) { eligibleGroups[i] = wmxDb.ConfigurationManager.UserGroups.get_Item(i).Name; } } else if (user.HasNamedPrivilege(ESRI.ArcGIS.JTX.Utilities.Constants.PRIV_GROUP_JOB_ASSIGN)) { eligibleGroups = new string[user.Groups.Count]; for (int i = 0; i < user.Groups.Count; i++) { eligibleGroups[i] = user.Groups.get_Item(i).Name; } } else { eligibleGroups = new string[0]; } // Sort the types first SortedList <string, string> sortedValues = new SortedList <string, string>(); for (int i = 0; i < eligibleGroups.Length; i++) { sortedValues.Add(eligibleGroups[i], null); } // Add the extra values, if any if (extraValues != null) { foreach (string s in extraValues) { sortedValues.Add(s, null); } } // Add the sorted types to the domain foreach (string value in sortedValues.Keys) { IGPValue tempGpVal = new GPStringClass(); tempGpVal.SetAsText(value); domain.AddCode(tempGpVal, value); } return(domain as IGPDomain); }
/// <summary> /// Find those users in the database who are not being referenced in any way /// </summary> /// <returns>The total number of orphaned items found</returns> private int UpdateOrphanedUsers() { SortedList <string, string> unusedItems = new SortedList <string, string>(); IJTXDatabase3 wmxDb = this.WmxDatabase; IJTXConfiguration3 configMgr = wmxDb.ConfigurationManager as IJTXConfiguration3; IJTXJobManager jobMgr = wmxDb.JobManager; IJTXUserSet allUsers = configMgr.Users; Dictionary <string, string> usedItems = new Dictionary <string, string>(); // Get all of the users who are members of a group IJTXUserGroupSet allGroups = configMgr.UserGroups; for (int i = 0; i < allGroups.Count; i++) { IJTXUserGroup2 group = allGroups.get_Item(i) as IJTXUserGroup2; for (int j = 0; j < group.Users.Count; j++) { IJTXUser3 user = group.Users.get_Item(j) as IJTXUser3; usedItems[user.UserName] = user.FullName; } } // If necessary, add in the users who have jobs assigned to them if (usedItems.Count < allUsers.Count) { IJTXJobSet allJobs = jobMgr.GetAllJobs(); for (int i = 0; i < allJobs.Count; i++) { IJTXJob3 job = allJobs.get_Item(i) as IJTXJob3; if (job.AssignedType == jtxAssignmentType.jtxAssignmentTypeUser) { IJTXUser3 user = configMgr.GetUser(job.AssignedTo) as IJTXUser3; // It's possible for a user to have a job assigned, but have // already been removed from the DB. Throw an exception in // this case, as the DB needs to be cleaned up. if (user == null) { throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR); } usedItems[user.UserName] = user.FullName; } } } // If necessary, add in the users who have a job type's default assignment // set to them if (usedItems.Count < allUsers.Count) { IJTXJobTypeSet allJobTypes = configMgr.JobTypes; for (int i = 0; i < allJobTypes.Count; i++) { // TODO: Exclude orphaned job types IJTXJobType3 jobType = allJobTypes.get_Item(i) as IJTXJobType3; if (jobType.DefaultAssignedType == jtxAssignmentType.jtxAssignmentTypeUser) { IJTXUser3 user = configMgr.GetUser(jobType.DefaultAssignedTo) as IJTXUser3; // It's possible for a user to have a job assigned, but have // already been removed from the DB. Throw an exception in // this case, as the DB needs to be cleaned up. if (user == null) { throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR); } usedItems[user.UserName] = user.FullName; } } } // If necessary, add in the users who have steps assigned to them // by default if (usedItems.Count < allUsers.Count) { IJTXWorkflowSet allWorkflows = configMgr.Workflows; for (int i = 0; i < allWorkflows.Count; i++) { // Skip over unused workflows IJTXWorkflow workflow = allWorkflows.get_Item(i); if (m_unusedWorkflows.Keys.Contains(workflow.ID)) { continue; } // Examine the other items IJTXWorkflowConfiguration workflowCfg = allWorkflows.get_Item(i) as IJTXWorkflowConfiguration; int[] workflowStepIds = workflowCfg.GetAllSteps(); foreach (int j in workflowStepIds) { IJTXStep3 step = workflowCfg.GetStep(j) as IJTXStep3; if (step.AssignedType == jtxAssignmentType.jtxAssignmentTypeUser) { IJTXUser3 user = configMgr.GetUser(step.AssignedTo) as IJTXUser3; // It's possible for a user to have a job assigned, but have // already been removed from the DB. Throw an exception in // this case, as the DB needs to be cleaned up. if (user == null) { throw new WmauException(WmauErrorCodes.C_USER_NOT_FOUND_ERROR); } usedItems[user.UserName] = user.FullName; } } } } // Loop over all the users in the DB, looking for anything // that we didn't identify as "in use" for (int i = 0; i < allUsers.Count; i++) { IJTXUser3 item = allUsers.get_Item(i) as IJTXUser3; if (!usedItems.ContainsKey(item.UserName)) { m_unusedUsers[item.UserName] = item.FullName; } } return(m_unusedUsers.Count); }
/// <summary> /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed. /// </summary> /// <param name="paramValues"></param> /// <param name="trackCancel"></param> /// <param name="envMgr"></param> /// <param name="msgs"></param> public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs) { // Do some common error-checking base.Execute(paramValues, trackCancel, envMgr, msgs); try { // Ensure that the current user has admin access to the current Workflow Manager DB if (!CurrentUserIsWmxAdministrator()) { throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR); } IJTXConfigurationEdit2 configEdit = this.WmxDatabase.ConfigurationManager as IJTXConfigurationEdit2; // Look up the appropriate user(s) IJTXUserSet users = null; if (m_userName.Equals(C_OPT_ALL_USERS)) { users = configEdit.Users; } else { users = new JTXUserSetClass(); (users as IJTXUserSetEdit).Add(configEdit.GetUser(m_userName)); } // Grant/revoke admin access to the specified users for (int i = 0; i < users.Count; i++) { IJTXUser3 user = users.get_Item(i) as IJTXUser3; user.IsAdministrator = m_privilegeAction.Equals(C_OPT_GRANT) ? true : false; (user as IJTXUserConfig).Store(); } // If the tool was set to preserve the current user's access and the tool removed it, // re-grant their access string username = ESRI.ArcGIS.JTXUI.ConfigurationCache.GetCurrentSystemUser(ESRI.ArcGIS.JTXUI.ConfigurationCache.UseUserDomain); IJTXUser3 userObj = configEdit.GetUser(username) as IJTXUser3; if (!userObj.IsAdministrator) { if (m_preserveCurrentUser) { userObj.IsAdministrator = true; (userObj as IJTXUserConfig).Store(); msgs.AddMessage("Re-granting admin access for user '" + username + "'"); } else { msgs.AddWarning("User '" + username + "' is no longer an administrator on this Workflow Manager database"); } } // Update the output parameter WmauParameterMap paramMap = new WmauParameterMap(paramValues); IGPParameterEdit3 outParam = paramMap.GetParamEdit(C_PARAM_OUT_USER_NAME); IGPString strValue = new GPStringClass(); strValue.Value = m_userName; outParam.Value = strValue as IGPValue; msgs.AddMessage(Properties.Resources.MSG_DONE); } catch (WmauException wmEx) { try { msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message); } catch { // Catch anything else that possibly happens } } catch (Exception ex) { try { WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR); msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message); } catch { // Catch anything else that possibly happens } } finally { // Release any COM objects here! } }