PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/Modules/PigeonCms.RolesAdmin/views/Default.ascx.cs

http://pigeoncms.googlecode.com/
C# | 270 lines | 232 code | 32 blank | 6 comment | 34 complexity | 22cc86cbdca27e41703f4451dfdb999e MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using System.Web.Caching;
 12using System.Collections.Generic;
 13using PigeonCms;
 14
 15public partial class Controls_Default : PigeonCms.BaseModuleControl
 16{
 17    private const int View_Grid_Index = 0;
 18    private const int View_Insert_Index = 1;
 19    private const int View_Users_Index = 2;
 20
 21    /// <summary>
 22    /// pkey, current role
 23    /// </summary>
 24    protected string CurrentRole
 25    {
 26        get
 27        {
 28            string res = "";
 29            if (ViewState["CurrentRole"] != null)
 30                res = (string)ViewState["CurrentRole"];
 31            return res;
 32        }
 33        set { ViewState["CurrentRole"] = value; }
 34    }
 35
 36    public class RoleName
 37    {
 38        public RoleName(string role)
 39        {
 40            this.Role = role;
 41        }
 42        public string Role { get; set; }
 43    }
 44
 45    protected void Page_Load(object sender, EventArgs e)
 46    {
 47        if (!Page.IsPostBack)
 48        {
 49            loadGrid();
 50        }
 51    }
 52
 53    protected void Grid1_RowCommand(object sender, GridViewCommandEventArgs e)
 54    {
 55        if (e.CommandName == "Select")
 56        {
 57            editRow(e.CommandArgument.ToString());
 58        }
 59        if (e.CommandName == "DeleteRow")
 60        {
 61            deleteRow(e.CommandArgument.ToString());
 62        }
 63    }
 64
 65    protected void Grid1_RowCreated(object sender, GridViewRowEventArgs e)
 66    {
 67        if (e.Row.RowType == DataControlRowType.Header)
 68            Utility.AddGlyph(Grid1, e.Row);
 69    }
 70
 71    protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
 72    {
 73        if (e.Row.RowType == DataControlRowType.DataRow)
 74        {
 75            RoleName currItem = (RoleName)e.Row.DataItem;
 76
 77            Literal LitNumUsersInRole = (Literal)e.Row.FindControl("LitNumUsersInRole");
 78            Literal LitUsersInRole = (Literal)e.Row.FindControl("LitUsersInRole");
 79
 80            LitNumUsersInRole.Text = Roles.GetUsersInRole(currItem.Role).Length.ToString();
 81
 82            string usersInRole = "";
 83            foreach (string item in Roles.GetUsersInRole(currItem.Role))
 84            {
 85                usersInRole += item + ", ";
 86            }
 87            if (usersInRole.Length > 0)
 88            {
 89                usersInRole = usersInRole.Remove(usersInRole.Length - 2);
 90            }
 91            LitUsersInRole.Text = Utility.Html.GetTextPreview(usersInRole, 500, "");
 92        }
 93    }
 94
 95    protected void Grid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 96    {
 97        Grid1.PageIndex = e.NewPageIndex;
 98        loadGrid();
 99    }
100
101    protected void BtnNew_Click(object sender, EventArgs e)
102    {
103        editRow("");
104    }
105
106    protected void BtnSave_Click(object sender, EventArgs e)
107    {
108        if (checkForm())
109        {
110            if (saveForm())
111                MultiView1.ActiveViewIndex = View_Grid_Index;
112        }
113    }
114
115    protected void BtnCancel_Click(object sender, EventArgs e)
116    {
117        LblErr.Text = "";
118        LblOk.Text = "";
119        MultiView1.ActiveViewIndex = View_Grid_Index;
120    }
121
122    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
123    { }
124
125    #region private methods
126
127    private bool checkForm()
128    {
129        LblErr.Text = "";
130        LblOk.Text = "";
131        bool res = true;
132
133        if (MultiView1.ActiveViewIndex == View_Insert_Index)    //insert
134        {
135            if (string.IsNullOrEmpty(TxtRolename.Text))
136            {
137                res = false;
138                LblErr.Text += "inserire in nome della role";
139            }
140        }
141        else if (MultiView1.ActiveViewIndex == View_Users_Index)    //users in role
142        {
143        }
144        return res;
145    }
146
147    private bool saveForm()
148    {
149        LblErr.Text = "";
150        LblOk.Text = "";
151        bool res = false;
152
153        try
154        {
155            if (MultiView1.ActiveViewIndex == View_Insert_Index)    //insert
156            {
157                if (CurrentRole == "")
158                {
159                    Roles.CreateRole(TxtRolename.Text);
160                    res = true;
161                }
162            }
163            if (MultiView1.ActiveViewIndex == View_Users_Index)    //users in role
164            {
165                if (CurrentRole != "")
166                {
167                    //remove all users from role
168                    string[] usersToRemove = Roles.GetUsersInRole(CurrentRole);
169                    if (usersToRemove.Length > 0)
170                        Roles.RemoveUsersFromRole(usersToRemove, CurrentRole);
171
172                    //add selected users to role
173                    if (HiddenUsersInRole.Value.Length > 0)
174                    {
175                        string[] usersToAdd = HiddenUsersInRole.Value.Split('|');
176                        Roles.AddUsersToRole(usersToAdd, CurrentRole);
177                    }
178                    res = true;
179                }
180            }
181
182            if (res)
183            {
184                loadGrid();
185                LblOk.Text = Utility.GetLabel("RECORD_SAVED_MSG");
186                MultiView1.ActiveViewIndex = View_Grid_Index;
187            }
188        }
189        catch (Exception e1)
190        {
191            LblErr.Text = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.Message;
192        }
193        finally
194        {
195        }
196        return res;
197    }
198
199    private void form2obj(string rolename)
200    {
201        throw new NotImplementedException();
202    }
203
204    private void editRow(string rolename)
205    {
206        LblOk.Text = "";
207        LblErr.Text = "";
208
209        clearForm();
210        CurrentRole = rolename;
211
212        if (rolename == string.Empty)
213        {
214            MultiView1.ActiveViewIndex = View_Insert_Index;
215        }
216        else
217        {
218            LitRolename.Text = CurrentRole;
219            PgnUserHelper.LoadListUsersInRole(ListUsersInRole, CurrentRole);
220            PgnUserHelper.LoadListUsersNotInRole(ListUsersNotInRole, CurrentRole);
221            //load hidden field with current roles
222            HiddenUsersInRole.Value = "";
223            foreach (ListItem item in ListUsersInRole.Items)
224            {
225                HiddenUsersInRole.Value += item.Value + "|";
226            }
227            if (HiddenUsersInRole.Value.Length > 0)
228                HiddenUsersInRole.Value =
229                    HiddenUsersInRole.Value.Remove(HiddenUsersInRole.Value.Length - 1);
230
231            MultiView1.ActiveViewIndex = View_Users_Index;
232        }
233    }
234
235    private void deleteRow(string rolename)
236    {
237        LblOk.Text = "";
238        LblErr.Text = "";
239
240        try
241        {
242            Roles.DeleteRole(rolename);
243        }
244        catch (Exception e)
245        {
246            LblErr.Text = e.Message;
247        }
248        loadGrid();
249    }
250
251    private void clearForm()
252    {
253        TxtRolename.Text = "";
254        LitRolename.Text = "";
255    }
256
257    private void loadGrid()
258    {
259        string[] roles = Roles.GetAllRoles();
260        List<RoleName> rolenames = new List<RoleName>();
261        foreach (string role in roles)
262        {
263            rolenames.Add(new RoleName(role));
264        }
265        Grid1.DataSource = rolenames;
266        Grid1.DataBind();
267    }
268
269    #endregion
270}