/// <summary>
    /// Delete given resource from database.
    /// </summary>
    /// <param name="sender">sender</param>
    /// <param name="e">Event Args</param>
    protected void DeleteButton_OnClick(object sender, EventArgs e)
    {
        using (ResourceDataAccess dataAccess = new ResourceDataAccess())
        {
            if (ResourceDetailView.ResourceId != Guid.Empty)
            {
                Resource resource     = dataAccess.GetResource(ResourceDetailView.ResourceId);
                bool     isAuthorized = resource is CategoryNode?
                                        dataAccess.AuthorizeUserForDeletePermissionOnCategory(ResourceDetailView.AuthenticatedToken, resource.Id) :
                                            dataAccess.AuthorizeUser(ResourceDetailView.AuthenticatedToken, UserResourcePermissions.Delete, resource.Id);

                if (isAuthorized)
                {
                    bool isDeleted = resource is CategoryNode?
                                     dataAccess.DeleteCategoryNodeWithHierarchy(ResourceDetailView.ResourceId) :
                                         dataAccess.DeleteResource(ResourceDetailView.ResourceId);

                    //Delete resource
                    if (isDeleted)
                    {
                        DeleteButton.Visible       = false;
                        ResourceDetailView.Visible = false;
                        //Show Delete successful message
                        MessageLabel.ForeColor = System.Drawing.Color.Black;
                        MessageLabel.Text      = Resources.Resources.AlertRecordDelerted;
                        if (OnSuccessfulDelete != null)
                        {
                            OnSuccessfulDelete(this, new EventArgs());
                        }
                    }
                    else
                    {
                        //Show delete failure message
                        MessageLabel.ForeColor = System.Drawing.Color.Red;
                        MessageLabel.Text      = Resources.Resources.AlertResourceDelertedError;
                    }
                }
                else
                {
                    //Show delete failure message
                    MessageLabel.ForeColor = System.Drawing.Color.Red;
                    if (resource is CategoryNode)
                    {
                        MessageLabel.Text = Resources.Resources.MsgUnauthorizeAccessDeleteCategory;
                    }
                    else
                    {
                        MessageLabel.Text = string.Format(CultureInfo.InvariantCulture, Resources.Resources.MsgUnAuthorizeAccess,
                                                          UserResourcePermissions.Delete);
                    }
                }
            }
        }
    }
Beispiel #2
0
        void ResourceListView_OnDeleteButtonClicked(object sender, DeleteEventArgs e)
        {
            if (e.EntityIdList != null && e.EntityIdList.Count > 0)
            {
                using (ResourceDataAccess resourceDAL = new ResourceDataAccess(this.CreateContext()))
                {
                    if (IsSecurityAwareControl)
                    {
                        Collection <Guid> resourceIdsToBeDeleted = new Collection <Guid>();

                        foreach (Guid resId in e.EntityIdList.ToList())
                        {
                            Resource resource = resourceDAL.GetResource(resId);

                            if (resource != null)
                            {
                                resourceIdsToBeDeleted.Add(resource.Id);
                                CategoryNode categoryNodeobject = resource as CategoryNode;

                                bool isCategoryNode = false;
                                if (categoryNodeobject != null)
                                {
                                    isCategoryNode = true;
                                }

                                if (IsSecurityAwareControl && AuthenticatedToken != null)
                                {
                                    bool isAuthorized = isCategoryNode ?
                                                        resourceDAL.AuthorizeUserForDeletePermissionOnCategory(AuthenticatedToken, resId) :
                                                        resourceDAL.AuthorizeUser(AuthenticatedToken, UserResourcePermissions.Delete, resId);

                                    if (!isAuthorized)
                                    {
                                        if (isCategoryNode)
                                        {
                                            throw new UnauthorizedAccessException(GlobalResource.UnauthorizedAccessExceptionCategoryDelete);
                                        }
                                        else
                                        {
                                            throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture,
                                                                                                GlobalResource.UnauthorizedAccessException, UserResourcePermissions.Delete));
                                        }
                                    }
                                }

                                if (isCategoryNode)
                                {
                                    UpdateEntityListWithCategoryNodes(categoryNodeobject, resourceIdsToBeDeleted);
                                }
                            }
                        }

                        resourceDAL.DeleteResources(resourceIdsToBeDeleted);
                    }
                }

                //Refresh data source for current page.
                Refresh();

                //If page count is changed and Data fetched is empty then try to fetch last page
                IList entityList = ResourceListView.DataSource as IList;
                if ((entityList == null || entityList.Count == 0) && ResourceListView.TotalRecords > 0)
                {
                    ResourceListView.PageIndex = Convert.ToInt32(Math.Ceiling((double)ResourceListView.TotalRecords / ResourceListView.PageSize)) - 1;
                    Refresh();
                }
            }
        }