private void RetrieveAccessRights() { if (cBoxEntities.SelectedIndex < 0) { MessageBox.Show(this, "Please select an entity", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtObjectId.Text.Length == 0) { MessageBox.Show(this, "Please specify an object Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (textBox_UserID.Text.Length == 0) { MessageBox.Show(this, "Please select a user", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { var g = new Guid(txtObjectId.Text); } catch { MessageBox.Show(this, "The object ID is invalid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var parameters = new List<string> {txtObjectId.Text, textBox_UserID.Tag.ToString(), ((EntityInfo)cBoxEntities.SelectedItem).LogicalName, ((EntityInfo)cBoxEntities.SelectedItem).PrimaryAttribute}; WorkAsync("Retrieving access rights...", e => { var crmAccess = new CrmAccess(Service); var inParameters = (List<string>)e.Argument; var recordId = new Guid(inParameters[0]); var userId = new Guid(inParameters[1]); var entityName = inParameters[2]; var primaryAttribute = inParameters[3]; var result = new CheckAccessResult(); RetrievePrincipalAccessResponse response = crmAccess.RetrieveRights( userId, recordId, entityName); Dictionary<string, Guid> privileges = crmAccess.RetrievePrivileges(entityName); // Get primary attribute value for the current record Entity de = crmAccess.RetrieveDynamicWithPrimaryAttr(recordId, entityName, primaryAttribute); result.RecordName = de.GetAttributeValue<string>(primaryAttribute); // Check Privileges foreach (string privlegeName in privileges.Keys) { if (privlegeName.StartsWith("prvappend")) { result.HasAppendAccess = (response.AccessRights & AccessRights.AppendAccess) == AccessRights.AppendAccess; result.AppendPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvappendto")) { result.HasAppendToAccess = (response.AccessRights & AccessRights.AppendToAccess) == AccessRights.AppendToAccess; result.AppendToPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvassign")) { result.HasAssignAccess = (response.AccessRights & AccessRights.AssignAccess) == AccessRights.AssignAccess; result.AssignPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvcreate")) { result.HasCreateAccess = (response.AccessRights & AccessRights.CreateAccess) == AccessRights.CreateAccess; result.CreatePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvdelete")) { result.HasDeleteAccess = (response.AccessRights & AccessRights.DeleteAccess) == AccessRights.DeleteAccess; result.DeletePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvread")) { result.HasReadAccess = (response.AccessRights & AccessRights.ReadAccess) == AccessRights.ReadAccess; result.ReadPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvshare")) { result.HasShareAccess = (response.AccessRights & AccessRights.ShareAccess) == AccessRights.ShareAccess; result.SharePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvwrite")) { result.HasWriteAccess = (response.AccessRights & AccessRights.WriteAccess) == AccessRights.WriteAccess; result.WritePrivilegeId = privileges[privlegeName]; } } e.Result = result; }, e => { if (e.Error != null) { MessageBox.Show(this, "An error occured: " + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var result = (CheckAccessResult)e.Result; var greenColor = Color.FromArgb(0, 158, 73); var redColor = Color.FromArgb(232, 17, 35); textBox_PrimaryAttribute.Text = result.RecordName; pnlAppend.BackColor = result.HasAppendAccess ? greenColor : result.AppendPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAppendTo.BackColor = result.HasAppendToAccess ? greenColor : result.AppendToPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAssign.BackColor = result.HasAssignAccess ? greenColor : result.AssignPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlCreate.BackColor = result.HasCreateAccess ? greenColor : result.CreatePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlDelete.BackColor = result.HasDeleteAccess ? greenColor : result.DeletePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlRead.BackColor = result.HasReadAccess ? greenColor : result.ReadPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlShare.BackColor = result.HasShareAccess ? greenColor : result.SharePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlWrite.BackColor = result.HasWriteAccess ? greenColor : result.WritePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; lblPrivilegeAppend.Text = result.AppendPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AppendPrivilegeId.ToString("B")); lblPrivilegeAppendTo.Text = result.AppendToPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AppendToPrivilegeId.ToString("B")); lblPrivilegeAssign.Text = result.AssignPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AssignPrivilegeId.ToString("B")); lblPrivilegeCreate.Text = result.CreatePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.CreatePrivilegeId.ToString("B")); lblPrivilegeDelete.Text = result.DeletePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.DeletePrivilegeId.ToString("B")); lblPrivilegeRead.Text = result.ReadPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.ReadPrivilegeId.ToString("B")); lblPrivilegeShare.Text = result.SharePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.SharePrivilegeId.ToString("B")); lblPrivilegeWrite.Text = result.WritePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.WritePrivilegeId.ToString("B")); } }, parameters); }
private void RetrieveAccessRights() { if (cBoxEntities.SelectedIndex < 0) { MessageBox.Show(this, "Please select an entity", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtObjectId.Text.Length == 0) { MessageBox.Show(this, "Please specify an object Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (textBox_UserID.Text.Length == 0) { MessageBox.Show(this, "Please select a user", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { var g = new Guid(txtObjectId.Text); } catch { MessageBox.Show(this, "The object ID is invalid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var parameters = new List <string> { txtObjectId.Text, textBox_UserID.Tag.ToString(), ((EntityInfo)cBoxEntities.SelectedItem).LogicalName, ((EntityInfo)cBoxEntities.SelectedItem).PrimaryAttribute }; WorkAsync(new WorkAsyncInfo { Message = "Retrieving access rights...", AsyncArgument = parameters, Work = (bw, e) => { var crmAccess = new CrmAccess(Service); var inParameters = (List <string>)e.Argument; var recordId = new Guid(inParameters[0]); var userId = new Guid(inParameters[1]); var entityName = inParameters[2]; var primaryAttribute = inParameters[3]; var result = new CheckAccessResult(); RetrievePrincipalAccessResponse response = crmAccess.RetrieveRights( userId, recordId, entityName); Dictionary <string, Guid> privileges = crmAccess.RetrievePrivileges(entityName); // Get primary attribute value for the current record Entity de = crmAccess.RetrieveDynamicWithPrimaryAttr(recordId, entityName, primaryAttribute); result.RecordName = de.GetAttributeValue <string>(primaryAttribute); // Check Privileges foreach (string privlegeName in privileges.Keys) { if (privlegeName.StartsWith("prvappend")) { result.HasAppendAccess = (response.AccessRights & AccessRights.AppendAccess) == AccessRights.AppendAccess; result.AppendPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvappendto")) { result.HasAppendToAccess = (response.AccessRights & AccessRights.AppendToAccess) == AccessRights.AppendToAccess; result.AppendToPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvassign")) { result.HasAssignAccess = (response.AccessRights & AccessRights.AssignAccess) == AccessRights.AssignAccess; result.AssignPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvcreate")) { result.HasCreateAccess = (response.AccessRights & AccessRights.CreateAccess) == AccessRights.CreateAccess; result.CreatePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvdelete")) { result.HasDeleteAccess = (response.AccessRights & AccessRights.DeleteAccess) == AccessRights.DeleteAccess; result.DeletePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvread")) { result.HasReadAccess = (response.AccessRights & AccessRights.ReadAccess) == AccessRights.ReadAccess; result.ReadPrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvshare")) { result.HasShareAccess = (response.AccessRights & AccessRights.ShareAccess) == AccessRights.ShareAccess; result.SharePrivilegeId = privileges[privlegeName]; } if (privlegeName.StartsWith("prvwrite")) { result.HasWriteAccess = (response.AccessRights & AccessRights.WriteAccess) == AccessRights.WriteAccess; result.WritePrivilegeId = privileges[privlegeName]; } } e.Result = result; }, PostWorkCallBack = e => { if (e.Error != null) { MessageBox.Show(this, "An error occured: " + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var result = (CheckAccessResult)e.Result; var greenColor = Color.FromArgb(0, 158, 73); var redColor = Color.FromArgb(232, 17, 35); textBox_PrimaryAttribute.Text = result.RecordName; pnlAppend.BackColor = result.HasAppendAccess ? greenColor : result.AppendPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAppendTo.BackColor = result.HasAppendToAccess ? greenColor : result.AppendToPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAssign.BackColor = result.HasAssignAccess ? greenColor : result.AssignPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlCreate.BackColor = result.HasCreateAccess ? greenColor : result.CreatePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlDelete.BackColor = result.HasDeleteAccess ? greenColor : result.DeletePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlRead.BackColor = result.HasReadAccess ? greenColor : result.ReadPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlShare.BackColor = result.HasShareAccess ? greenColor : result.SharePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlWrite.BackColor = result.HasWriteAccess ? greenColor : result.WritePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; lblPrivilegeAppend.Text = result.AppendPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AppendPrivilegeId.ToString("B")); lblPrivilegeAppendTo.Text = result.AppendToPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AppendToPrivilegeId.ToString("B")); lblPrivilegeAssign.Text = result.AssignPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.AssignPrivilegeId.ToString("B")); lblPrivilegeCreate.Text = result.CreatePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.CreatePrivilegeId.ToString("B")); lblPrivilegeDelete.Text = result.DeletePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.DeletePrivilegeId.ToString("B")); lblPrivilegeRead.Text = result.ReadPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.ReadPrivilegeId.ToString("B")); lblPrivilegeShare.Text = result.SharePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.SharePrivilegeId.ToString("B")); lblPrivilegeWrite.Text = result.WritePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : string.Format("Privilege Id: {0}", result.WritePrivilegeId.ToString("B")); } } }); }
private void RetrieveAccessRights() { if (cBoxEntities.SelectedIndex < 0) { MessageBox.Show(this, "Please select an table", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtObjectId.Text.Length == 0) { MessageBox.Show(this, "Please specify an object Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (textBox_UserID.Text.Length == 0) { MessageBox.Show(this, "Please select a user", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { var g = new Guid(txtObjectId.Text); } catch { MessageBox.Show(this, "The object ID is invalid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } ResetPermissions(); var parameters = new List <object> { txtObjectId.Text, textBox_UserID.Tag, (EntityInfo)cBoxEntities.SelectedItem }; WorkAsync(new WorkAsyncInfo { Message = "Retrieving access rights...", AsyncArgument = parameters, Work = (bw, e) => { var crmAccess = new CrmAccess(Service, ConnectionDetail); var inParameters = (List <object>)e.Argument; var recordId = new Guid(inParameters[0].ToString()); var user = (User)inParameters[1]; var entityInfo = (EntityInfo)inParameters[2]; var entityName = entityInfo.LogicalName; var primaryAttribute = entityInfo.PrimaryAttribute; var result = new CheckAccessResult(); if (ConnectionDetail.NewAuthType != Microsoft.Xrm.Tooling.Connector.AuthenticationType.OAuth && ConnectionDetail.NewAuthType != Microsoft.Xrm.Tooling.Connector.AuthenticationType.Certificate) { RetrievePrincipalAccessResponse response = crmAccess.RetrieveRights( user.Id, recordId, entityName); Dictionary <string, Guid> privileges = crmAccess.RetrievePrivileges(entityName); // Get primary attribute value for the current record Entity de = crmAccess.RetrieveDynamicWithPrimaryAttr(recordId, entityName, primaryAttribute); result.RecordName = de.GetAttributeValue <string>(primaryAttribute); // Check Privileges foreach (string privlegeName in privileges.Keys) { if (privlegeName.StartsWith("prvappendto")) { result.HasAppendToAccess = (response.AccessRights & AccessRights.AppendToAccess) == AccessRights.AppendToAccess; result.AppendToPrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvappend")) { result.HasAppendAccess = (response.AccessRights & AccessRights.AppendAccess) == AccessRights.AppendAccess; result.AppendPrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvassign")) { result.HasAssignAccess = (response.AccessRights & AccessRights.AssignAccess) == AccessRights.AssignAccess; result.AssignPrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvcreate")) { result.HasCreateAccess = (response.AccessRights & AccessRights.CreateAccess) == AccessRights.CreateAccess; result.CreatePrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvdelete")) { result.HasDeleteAccess = (response.AccessRights & AccessRights.DeleteAccess) == AccessRights.DeleteAccess; result.DeletePrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvread")) { result.HasReadAccess = (response.AccessRights & AccessRights.ReadAccess) == AccessRights.ReadAccess; result.ReadPrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvshare")) { result.HasShareAccess = (response.AccessRights & AccessRights.ShareAccess) == AccessRights.ShareAccess; result.SharePrivilegeId = privileges[privlegeName]; } else if (privlegeName.StartsWith("prvwrite")) { result.HasWriteAccess = (response.AccessRights & AccessRights.WriteAccess) == AccessRights.WriteAccess; result.WritePrivilegeId = privileges[privlegeName]; } e.Result = result; } } else { List <Privilege> privList = crmAccess.RetrieveRightsAPI(user.Id, recordId, entityName); if (privList.Any(priv => priv.Permissions.Any(per => per.PermissionType == PermissionType.Role))) { crmAccess.GetRoleDetail(privList, user, recordId, entityInfo); } if (privList.Any(priv => priv.Permissions.Any(per => per.PermissionType == PermissionType.Shared))) { crmAccess.GetShareDetail(privList, user, recordId, entityInfo); } e.Result = privList; } }, PostWorkCallBack = e => { if (e.Error != null) { MessageBox.Show(this, "An error occured: " + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { if (e.Result is List <Privilege> privList) { var greenColor = Color.LightGreen; // Color.FromArgb(0, 158, 73); var redColor = Color.LightPink; // Color.FromArgb(232, 17, 35); foreach (Privilege privilege in privList) { Control panel = Helper.FindByTag(splitAccess, privilege.PrivilegeType); panel.BackColor = privilege.HasAccess ? greenColor : redColor; var privLabel = (TextBox)Helper.FindByTag(panel, privilege.PrivilegeType + "Label"); privLabel.BackColor = panel.BackColor; privLabel.Text = privilege.PrivilegeId.ToString(); FlowLayoutPanel flow = Helper.FindByTag(panel, privilege.PrivilegeType + "Flow") as FlowLayoutPanel; //flow.Controls.Clear(); if (flow != null) { foreach (var permission in privilege.Permissions.Where(per => per.PermissionType != PermissionType.Role && per.PermissionType != PermissionType.Shared)) { flow.Controls.Add(new DisplayAccess(permission)); } } } } else { var result = (CheckAccessResult)e.Result; var greenColor = Color.FromArgb(0, 158, 73); var redColor = Color.FromArgb(232, 17, 35); textBox_PrimaryAttribute.Text = result.RecordName; pnlAppend.BackColor = result.HasAppendAccess ? greenColor : result.AppendPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAppendTo.BackColor = result.HasAppendToAccess ? greenColor : result.AppendToPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlAssign.BackColor = result.HasAssignAccess ? greenColor : result.AssignPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlCreate.BackColor = result.HasCreateAccess ? greenColor : result.CreatePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlDelete.BackColor = result.HasDeleteAccess ? greenColor : result.DeletePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlRead.BackColor = result.HasReadAccess ? greenColor : result.ReadPrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlShare.BackColor = result.HasShareAccess ? greenColor : result.SharePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; pnlWrite.BackColor = result.HasWriteAccess ? greenColor : result.WritePrivilegeId != Guid.Empty ? redColor : Color.DarkGray; txtAppendPriv.BackColor = pnlAppend.BackColor; txtAppendPriv.Text = result.AppendPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.AppendPrivilegeId.ToString(); txtAppendToPriv.BackColor = pnlAppendTo.BackColor; txtAppendToPriv.Text = result.AppendToPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.AppendToPrivilegeId.ToString(); txtAssignPriv.BackColor = pnlAssign.BackColor; txtAssignPriv.Text = result.AssignPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.AssignPrivilegeId.ToString(); txtCreatePriv.BackColor = pnlCreate.BackColor; txtCreatePriv.Text = result.CreatePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.CreatePrivilegeId.ToString(); txtDeletePriv.BackColor = pnlDelete.BackColor; txtDeletePriv.Text = result.DeletePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.DeletePrivilegeId.ToString(); txtReadPriv.BackColor = pnlRead.BackColor; txtReadPriv.Text = result.ReadPrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.ReadPrivilegeId.ToString(); txtSharePriv.BackColor = pnlShare.BackColor; txtSharePriv.Text = result.SharePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.SharePrivilegeId.ToString(); txtWritePriv.BackColor = pnlWrite.BackColor; txtWritePriv.Text = result.WritePrivilegeId == Guid.Empty ? "Privilege does not exist for this entity" : result.WritePrivilegeId.ToString(); } ai.WriteEvent("User Checked", 1); } } }); }