Guushuuse .NET

领域驱动设计==哲学

导航

ASP.NET&Spring.NET&NHibernate最佳实践(二十四)——第4章权限子系统(17)

修改Users.aspx
<asp:Content ID="MiddleColumnContent" ContentPlaceHolderID="MiddleColumnZone" runat="server">
    
<asp:Panel ID="Panel1" runat="server" GroupingText="用户列表">
        
<br />
        
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="UserName"
            AutoGenerateColumns
="False" Width="100%" OnRowCommand="GridView1_RowCommand">
            
<Columns>
                
<asp:BoundField HeaderText="登录名" DataField="UserName" ReadOnly="true" />
                
<asp:HyperLinkField HeaderText="电子邮件地址" DataTextField="Email" DataNavigateUrlFormatString="mailto:{0}"
                    DataNavigateUrlFields
="Email" />
                
<asp:BoundField HeaderText="创建时间" DataField="CreationDate" DataFormatString="{0:MM/dd/yy h:mm tt}"
                    ReadOnly
="true" />
                
<asp:BoundField HeaderText="活动时间" DataField="LastActivityDate" DataFormatString="{0:MM/dd/yy h:mm tt}"
                    ReadOnly
="true" />
                
<asp:CheckBoxField HeaderText="审核" DataField="IsApproved" HeaderStyle-HorizontalAlign="Center"
                    ItemStyle-HorizontalAlign
="Center" />
                
<asp:TemplateField>
                    
<ItemTemplate>
                        
<div style="width: 100%; text-align: right;">
                            
<asp:LinkButton ID="btnEdit" CommandName="Edit" runat="server">编辑</asp:LinkButton>
                            
<asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('您真的要删除吗?')">删除</asp:LinkButton>
                        
</div>
                    
</ItemTemplate>
                    
<EditItemTemplate>
                        
<div style="width: 100%; text-align: right;">
                            
<asp:Button ID="btnUpdate" runat="server" CommandName="UpdateUser" CommandArgument="<%# Container.DataItemIndex %>"
                                Text
="更新" />
                            
<asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="取消" />
                        
</div>
                    
</EditItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
    
</asp:Panel>
    
<hr />
    
<asp:Panel ID="Panel2" runat="server" GroupingText="新增部用户">
        
<br />
        
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" ContinueDestinationPageUrl="~/Users.aspx"
            DisableCreatedUser
="True" EmailRegularExpression="" EmailRegularExpressionErrorMessage="">
        
</asp:CreateUserWizard>
    
</asp:Panel>
    
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="System.Web.Security.Membership"
        SelectMethod
="GetAllUsers" DeleteMethod="DeleteUser"></asp:ObjectDataSource>
</asp:Content>

修改Users.aspx..cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Users : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }


    
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    
{
        
if (e.CommandName == "UpdateUser")
        
{
            MembershipUser user 
= Membership.GetUser((string)GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

            
if (((CheckBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[4].Controls[0]).Checked)
            
{
                user.IsApproved 
= true;
            }

            
else
            
{
                user.IsApproved 
= false;
            }


            Membership.UpdateUser(user);

            GridView1.EditIndex 
= -1;

            
return;
        }


    }
     
}


修改UserRole.aspx
<asp:Content ID="MiddleColumnContent" ContentPlaceHolderID="MiddleColumnZone" runat="server">
    
<asp:Panel ID="Panel1" runat="server" GroupingText="权限设置">
        
<br />
        选择用户
<br />
        
<asp:DropDownList ID="lstUsers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="lstUsers_SelectedIndexChanged">
        
</asp:DropDownList>
        
<br />
        所有角色
<br />
        
<asp:CheckBoxList ID="chkRoles" runat="server">
        
</asp:CheckBoxList>
        
<p>
            
<asp:Button ID="btnUpdate" runat="server" Text="更新" OnClick="btnUpdate_Click" />
        
</p>
    
</asp:Panel>
</asp:Content>

修改UserRole.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserRole : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            BindUsers();
            BindRoles();
        }

    }


    
private void BindUsers()
    
{
        System.Web.Security.MembershipUserCollection users 
= System.Web.Security.Membership.GetAllUsers();

        lstUsers.DataSource 
= users;
        lstUsers.DataTextField 
= "UserName";
        lstUsers.DataValueField 
= "UserName";
        lstUsers.DataBind();
    }


    
private void BindRoles()
    
{
        
        chkRoles.Items.Clear();

        
string[] roles = System.Web.Security.Roles.GetAllRoles();

        
string[] userRoles = System.Web.Security.Roles.GetRolesForUser(lstUsers.SelectedValue);

        
foreach (string role in roles)
        
{
            ListItem item 
= new ListItem();
            item.Text 
= role;
            item.Value 
= role;

            
if (Array.IndexOf(userRoles, role) > -1)
            
{
                item.Selected 
= true;
            }


            chkRoles.Items.Add(item);
        }

    
    }


    
protected void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
    
{
        BindRoles();
    }


    
protected void btnUpdate_Click(object sender, EventArgs e)
    
{

        
foreach (ListItem item in chkRoles.Items)
        
{
            
if (item.Selected)
            
{
                System.Web.Security.Roles.AddUserToRole(lstUsers.SelectedValue, item.Value);
            }

            
else
            
{
                System.Web.Security.Roles.RemoveUserFromRole(lstUsers.SelectedValue, item.Value);
            }

        }


    }

}

posted on 2008-05-17 22:00  guushuuse  阅读(841)  评论(1编辑  收藏  举报