ACL.ascx.cs :  » Web-Frameworks » nopCommerce » NopSolutions » NopCommerce » Web » Administration » Modules » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » nopCommerce 
nopCommerce » NopSolutions » NopCommerce » Web » Administration » Modules » ACL.ascx.cs
//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Content.Polls;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.Security;
 
namespace NopSolutions.NopCommerce.Web.Administration.Modules{
    public partial class ACLControl : BaseNopAdministrationUserControl
    {
        public class NopGridViewCustomTemplate : ITemplate
        {
            #region Fields
            private DataControlRowType templateType;
            private string columnName;
            private string dataType;
            private int customerRoleId;
            #endregion

            #region Ctor
            public NopGridViewCustomTemplate(DataControlRowType type,
                string columnName, string DataType)
                : this(type, columnName, DataType, 0)
            {
            }

            public NopGridViewCustomTemplate(DataControlRowType type,
                string columnName, string DataType, int customerRoleId)
            {
                this.templateType = type;
                this.columnName = columnName;
                this.dataType = DataType;
                this.customerRoleId = customerRoleId;
            }
            #endregion

            #region Utilities
            private void ctrl_DataBinding(Object sender, EventArgs e)
            {
                WebControl ctrl = (WebControl)sender;
                GridViewRow row = (GridViewRow)ctrl.NamingContainer;
                switch (dataType)
                {
                    case "String":
                        {
                            object RawValue = DataBinder.Eval(row.DataItem, columnName);
                            ((Label)ctrl).Text = RawValue.ToString();
                        }
                        break;
                    case "Checkbox":
                        {
                            CustomerActionACLMappingHelperClass map1 = (CustomerActionACLMappingHelperClass)row.DataItem;
                            ((CheckBox)ctrl).Checked = map1.Allow[this.customerRoleId];
                        }
                        break;
                }
            }

            private void hfCustomerActionId_DataBinding(Object sender, EventArgs e)
            {
                HiddenField hf = (HiddenField)sender;
                GridViewRow row = (GridViewRow)hf.NamingContainer;

                CustomerActionACLMappingHelperClass map1 = (CustomerActionACLMappingHelperClass)row.DataItem;
                hf.Value = map1.CustomerActionId.ToString();
            }
            #endregion

            #region Methods
            public void InstantiateIn(Control container)
            {
                switch (templateType)
                {
                    case DataControlRowType.Header:
                        Literal lc = new Literal();
                        lc.Text = "<b>" + columnName + "</b>";
                        container.Controls.Add(lc);
                        break;
                    case DataControlRowType.DataRow:
                        WebControl ctrl = null;
                        switch (dataType)
                        {
                            case "String":
                                ctrl = new Label();
                                break;
                            case "Checkbox":
                                ctrl = new CheckBox();
                                ctrl.ID = string.Format("cbAllow_{0}", customerRoleId);

                                HiddenField hfCustomerActionId = new HiddenField();
                                hfCustomerActionId.ID = string.Format("hfCustomerActionId_{0}", customerRoleId);
                                hfCustomerActionId.DataBinding += new EventHandler(this.hfCustomerActionId_DataBinding);
                                container.Controls.Add(hfCustomerActionId);
                                break;
                            default:
                                throw new Exception("Not supported column type");
                                break;
                        }
                        ctrl.DataBinding += new EventHandler(this.ctrl_DataBinding);
                        container.Controls.Add(ctrl);
                        break;
                    default:
                        break;
                }
            }
            #endregion
        }

        protected class CustomerActionACLMappingHelperClass
        {
            public int CustomerActionId { get; set; }
            public string CustomerActionName { get; set; }
            public Dictionary<int, bool> Allow { get; set; }
        }

        protected void BuildColumnsDynamically()
        {
            gvACL.Columns.Clear();

            TemplateField tfAction = new TemplateField();
            tfAction.ItemTemplate = new NopGridViewCustomTemplate(DataControlRowType.DataRow, "CustomerActionName", "String");
            tfAction.HeaderTemplate = new NopGridViewCustomTemplate(DataControlRowType.Header, GetLocaleResourceString("Admin.ACL.Grid.CustomerAction"), "String");
            gvACL.Columns.Add(tfAction);

            CustomerRoleCollection roles = CustomerManager.GetAllCustomerRoles();
            foreach (CustomerRole cr in roles)
            {
                TemplateField tf = new TemplateField();
                tf.ItemTemplate = new NopGridViewCustomTemplate(DataControlRowType.DataRow, "Allow", "Checkbox", cr.CustomerRoleId);
                tf.HeaderTemplate = new NopGridViewCustomTemplate(DataControlRowType.Header, cr.Name, "String");
                gvACL.Columns.Add(tf);
            }
        }

        protected void BindGrid()
        {
            CustomerActionCollection actions = ACLManager.GetAllCustomerActions();
            if (actions.Count == 0)
            {
                lblMessage.Text = GetLocaleResourceString("Admin.ACL.NoActionDefined");
                return;
            }
            CustomerRoleCollection roles = CustomerManager.GetAllCustomerRoles();
            if (roles.Count == 0)
            {
                lblMessage.Text = GetLocaleResourceString("Admin.ACL.NoRolesDefined");
                return;
            }
            List<CustomerActionACLMappingHelperClass> dt = new List<CustomerActionACLMappingHelperClass>();
            foreach (CustomerAction ca in actions)
            {
                CustomerActionACLMappingHelperClass map1 = new CustomerActionACLMappingHelperClass();
                map1.CustomerActionId = ca.CustomerActionId;
                map1.CustomerActionName = ca.Name;
                map1.Allow = new Dictionary<int, bool>();

                foreach (CustomerRole cr in roles)
                {
                    ACLCollection acls = ACLManager.GetAllAcl(ca.CustomerActionId, cr.CustomerRoleId, null);
                    if (acls.Count > 0)
                    {
                        ACL acl = acls[0];
                        map1.Allow.Add(cr.CustomerRoleId, acl.Allow);
                    }
                    else
                    {
                        map1.Allow.Add(cr.CustomerRoleId, false);
                    }
                }
                dt.Add(map1);
            }
            
            gvACL.DataSource = dt;
            gvACL.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            BuildColumnsDynamically();
            base.OnInit(e);
        }
                
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
                BindData();
            }
        }

        private void BindData()
        {
            cbACLEnabled.Checked = SettingManager.GetSettingValueBoolean("ACL.Enabled");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    CustomerRoleCollection roles = CustomerManager.GetAllCustomerRoles();
                    if (roles.Count == 0)
                    {
                        lblMessage.Text = GetLocaleResourceString("Admin.ACL.NoRolesDefined");
                        return;
                    }
                    SettingManager.SetParam("ACL.Enabled", cbACLEnabled.Checked.ToString());

                    foreach (GridViewRow row in gvACL.Rows)
                    {
                        foreach (CustomerRole cr in roles)
                        {
                            CheckBox cbAllow = row.FindControl(string.Format("cbAllow_{0}", cr.CustomerRoleId)) as CheckBox;
                            HiddenField hfCustomerActionId = row.FindControl(string.Format("hfCustomerActionId_{0}", cr.CustomerRoleId)) as HiddenField;
                            if (cbAllow == null || hfCustomerActionId == null || String.IsNullOrEmpty(hfCustomerActionId.Value))
                                return;

                            bool allow = cbAllow.Checked;
                            int customerActionId = int.Parse(hfCustomerActionId.Value);

                            ACLCollection acls = ACLManager.GetAllAcl(customerActionId, cr.CustomerRoleId, null);
                            if (acls.Count > 0)
                            {
                                ACL acl = acls[0];
                                ACLManager.UpdateAcl(acl.AclId, customerActionId, cr.CustomerRoleId, allow);
                            }
                            else
                            {
                                ACL acl = ACLManager.InsertAcl(customerActionId, cr.CustomerRoleId, allow);
                            }
                        }
                    } 

                    Response.Redirect("ACL.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.