Exemple #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfAnonymous();

            if (ContactToEdit == null)
            {
                RecordNotFoundError.Visible = true;

                ManagePermissions.Visible = false;

                return;
            }

            var parentAccount            = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);
            var accountAccessPermissions = parentAccount == null ? new List <Entity>() : ServiceContext.GetAccountAccessByContact(Contact).ToList();
            var accountAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : accountAccessPermissions.Where(a => a.GetAttributeValue <EntityReference>("adx_accountid") != null && a.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference())).ToList();
            var contactAccessPermissions = parentAccount == null ? new List <Entity>() : XrmContext.GetContactAccessByContact(Contact).ToList();
            var contactAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : contactAccessPermissions.Where(c => c.GetAttributeValue <EntityReference>("adx_accountid") != null && c.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference()) && c.GetAttributeValue <OptionSetValue>("adx_scope").Value == (int)Enums.ContactAccessScope.Account).ToList();
            var channelPermissions     = XrmContext.GetChannelAccessByContact(Contact);
            var opportunityPermissions = XrmContext.GetOpportunityAccessByContact(Contact).FirstOrDefault();
            var contactToEditOpportunityPermissions = XrmContext.CreateQuery("adx_opportunitypermissions").FirstOrDefault(c => c.GetAttributeValue <EntityReference>("adx_contactid") != null && c.GetAttributeValue <EntityReference>("adx_contactid").Equals(ContactToEdit.ToEntityReference()));
            var contactToEditChannelPermissions     = XrmContext.CreateQuery("adx_channelpermissions").FirstOrDefault(c => c.GetAttributeValue <EntityReference>("adx_contactid") != null && c.GetAttributeValue <EntityReference>("adx_contactid").Equals(ContactToEdit.ToEntityReference()));
            var canEditContacts               = false;
            var canManagePermissions          = false;
            var canEditChannelPermissions     = channelPermissions != null;
            var canEditOpportunityPermissions = opportunityPermissions != null;

            if (parentAccount == null)
            {
                ManagePermissions.Visible = false;

                NoParentAccountError.Visible = true;

                return;
            }

            if (!accountAccessPermissions.Any())
            {
                ManagePermissions.Visible = false;

                NoAccountAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!accountAccessPermissionsForParentAccount.Any())
            {
                ManagePermissions.Visible = false;

                NoAccountAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            if (!contactAccessPermissions.Any())
            {
                ManagePermissions.Visible = false;

                NoContactAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!contactAccessPermissionsForParentAccount.Any())
            {
                ManagePermissions.Visible = false;

                NoContactAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            foreach (var access in accountAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_managepermissions").GetValueOrDefault(false))
                {
                    canManagePermissions = true;
                }
            }

            if (!canManagePermissions)
            {
                ManagePermissions.Visible = false;

                AccountAccessManagePermissionsDeniedError.Visible = true;

                return;
            }

            foreach (var access in contactAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false))
                {
                    canEditContacts = true;
                }
            }

            if (!canEditContacts)
            {
                ManagePermissions.Visible = false;

                ContactAccessPermissionsError.Visible = true;

                return;
            }

            if (channelPermissions == null && opportunityPermissions == null)
            {
                ManagePermissions.Visible = false;

                NoPermissionsError.Visible = true;

                return;
            }

            if (contactToEditOpportunityPermissions == null & contactToEditChannelPermissions == null)
            {
                ManagePermissions.Visible = false;

                PermissionsRecordsForContactEditNotFoundError.Visible = true;

                return;
            }

            if (!canEditOpportunityPermissions)
            {
                OpportunityPermissions.Visible = false;

                NoOpportunityPermissionsRecordWarning.Visible = true;
            }
            else
            {
                if (contactToEditOpportunityPermissions == null)
                {
                    OpportunityPermissions.Visible = false;

                    OpportunityPermissionsRecordForContactEditNotFoundError.Visible = true;
                }
                else
                {
                    var opportunityPermissionsFormViewDataSource = new CrmDataSource {
                        ID = "OpportunityPermissionsFormViewDataSource", CrmDataContextName = OpportunityPermissionsFormView.ContextName
                    };

                    var opportunityPermissionsFetchXml =
                        string.Format(
                            "<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>",
                            "adx_opportunitypermissions", "adx_opportunitypermissionsid", contactToEditOpportunityPermissions.Id);

                    opportunityPermissionsFormViewDataSource.FetchXml = opportunityPermissionsFetchXml;

                    OpportunityPermissions.Controls.Add(opportunityPermissionsFormViewDataSource);

                    OpportunityPermissionsFormView.DataSourceID = "OpportunityPermissionsFormViewDataSource";
                }
            }

            if (!canEditChannelPermissions)
            {
                ChannelPermissions.Visible = false;

                NoChannelPermissionsRecordWarning.Visible = true;
            }
            else
            {
                if (contactToEditChannelPermissions == null)
                {
                    ChannelPermissions.Visible = false;

                    ChannelPermissionsRecordForContactEditNotFoundError.Visible = true;
                }
                else
                {
                    var channelPermissionsFormViewDataSource = new CrmDataSource {
                        ID = "ChannelPermissionsWebViewDataSource", CrmDataContextName = ChannelPermissionsFormView.ContextName
                    };

                    var channelPermissionsFetchXml =
                        string.Format(
                            "<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>",
                            "adx_channelpermissions", "adx_channelpermissionsid", contactToEditChannelPermissions.Id);

                    channelPermissionsFormViewDataSource.FetchXml = channelPermissionsFetchXml;

                    ChannelPermissions.Controls.Add(channelPermissionsFormViewDataSource);

                    ChannelPermissionsFormView.DataSourceID = "ChannelPermissionsWebViewDataSource";
                }
            }

            if (!IsPostBack)
            {
                SetControlValues(contactToEditOpportunityPermissions, contactToEditChannelPermissions);
            }

            EnableControlsBasedOnPartnerAccess(opportunityPermissions, channelPermissions);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfNecessary();

            if (ContactToEdit == null)
            {
                RecordNotFoundError.Visible = true;

                ContactInformation.Visible = false;

                return;
            }

            var parentAccount = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);
            var accountAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : XrmContext.GetAccountAccessByContact(Contact).Where(a => a.GetAttributeValue <EntityReference>("adx_accountid") != null && a.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference())).ToList();
            var contactAccessPermissions = parentAccount == null ? new List <Entity>() : XrmContext.GetContactAccessByContact(Contact).ToList();
            var contactAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : contactAccessPermissions.Where(c => c.GetAttributeValue <EntityReference>("adx_accountid") != null && c.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference()) && c.GetAttributeValue <OptionSetValue>("adx_scope").Value == (int)Enums.ContactAccessScope.Account).ToList();
            var canEditContacts      = false;
            var canReadContacts      = false;
            var canManagePermissions = false;

            if (parentAccount == null)
            {
                ContactInformation.Visible = false;

                NoParentAccountError.Visible = true;

                return;
            }

            if (!contactAccessPermissions.Any())
            {
                ContactInformation.Visible = false;

                NoContactAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!contactAccessPermissionsForParentAccount.Any())
            {
                ContactInformation.Visible = false;

                NoContactAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            foreach (var access in contactAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false))
                {
                    canEditContacts = true;
                    canReadContacts = true;
                }
                if (access.GetAttributeValue <bool?>("adx_read").GetValueOrDefault(false))
                {
                    canReadContacts = true;
                }
            }

            foreach (var access in accountAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_managepermissions").GetValueOrDefault(false))
                {
                    canManagePermissions = true;
                }
            }

            if (!canReadContacts)
            {
                ContactInformation.Visible = false;

                return;
            }

            if (!canEditContacts)
            {
                ContactEditFormView.Visible = false;

                ContactReadOnlyFormView.Visible = true;

                ContactAccessWritePermissionDeniedMessage.Visible = true;
            }

            ManagePermissionsButton.Visible = canManagePermissions;

            var formViewDataSource = new CrmDataSource {
                ID = "WebFormDataSource", CrmDataContextName = ContactEditFormView.ContextName
            };

            var fetchXml = string.Format("<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>", "contact", "contactid", ContactToEdit.GetAttributeValue <Guid>("contactid"));

            formViewDataSource.FetchXml = fetchXml;

            ContactInformation.Controls.Add(formViewDataSource);

            ContactEditFormView.DataSourceID = "WebFormDataSource";

            ContactReadOnlyFormView.DataSourceID = "WebFormDataSource";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfNecessary();

            var parentAccount            = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : XrmContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);
            var contactAccessPermissions = parentAccount == null ? new List <Entity>() : XrmContext.GetContactAccessByContact(Contact).ToList();
            var contactAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : contactAccessPermissions.Where(c => c.GetAttributeValue <EntityReference>("adx_accountid") != null && c.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference()) && c.GetAttributeValue <OptionSetValue>("adx_scope").Value == (int)Enums.ContactAccessScope.Account).ToList();
            var canCreateContacts = false;

            if (parentAccount == null)
            {
                ContactWebForm.Visible = false;

                NoParentAccountError.Visible = true;

                return;
            }

            if (!contactAccessPermissions.Any())
            {
                ContactWebForm.Visible = false;

                NoContactAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!contactAccessPermissionsForParentAccount.Any())
            {
                ContactWebForm.Visible = false;

                NoContactAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            foreach (var access in contactAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_create").GetValueOrDefault(false))
                {
                    canCreateContacts = true;
                }
            }

            if (!canCreateContacts)
            {
                ContactWebForm.Visible = false;

                ContactAccessPermissionsError.Visible = true;
            }
        }