DataSetのコードは省略し、DACと、UIのコードを。
まずは、DAC(~/DAC/UserInfo.cs)から。
using System.Collections.Generic;
using System.Web.Security;
using Library.DataSet;
using Library.DataSet.LibraryDataSetTableAdapters;
namespace Library.DAC
{
public static class UserInfo
{
public static LibraryDataSet.UsersDataTable GetAllData()
{
LibraryDataSet.UsersDataTable dt = new LibraryDataSet.UsersDataTable();
using (UsersTableAdapter ta = new UsersTableAdapter())
{
dt = ta.GetData();
foreach (MembershipUser msu in Membership.GetAllUsers())
{
if ((dt.Select("UserName='" + msu.UserName + "'")).Length == 0)
{
dt.AddUsersRow(msu.UserName, string.Empty, false, false);
ta.Update(dt);
}
}
dt = ta.GetData();
foreach (LibraryDataSet.UsersRow ur in dt)
{
ur.AdminRoleFlag = Roles.IsUserInRole(ur.UserName, "Admins");
ur.UserRoleFlag = Roles.IsUserInRole(ur.UserName, "Users");
}
return dt;
}
}
public static void Update(string userName, string information, bool adminRoleFlag, bool userRoleFlag)
{
using (UsersTableAdapter ta = new UsersTableAdapter())
{
ta.Update(information, userName);
}
if (adminRoleFlag && !Roles.IsUserInRole(userName, "Admins"))
{
Roles.AddUserToRole(userName, "Admins");
}
else if (!adminRoleFlag && Roles.IsUserInRole(userName, "Admins"))
{
Roles.RemoveUserFromRole(userName, "Admins");
}
if (userRoleFlag && !Roles.IsUserInRole(userName, "Users"))
{
Roles.AddUserToRole(userName, "Users");
}
else if (!userRoleFlag && Roles.IsUserInRole(userName, "Users"))
{
Roles.RemoveUserFromRole(userName, "Users");
}
}
public static void InheritBookOwner(string oldOwner, string newOwner)
{
using (BookOwnerTableAdapter ta = new BookOwnerTableAdapter())
{
if (string.IsNullOrEmpty(newOwner))
{
ta.DeleteByUserName(oldOwner);
}
else
{
ta.InheritBookOwner(newOwner, oldOwner);
}
}
}
public static void DeleteUser(string userName)
{
using (UsersTableAdapter ta = new UsersTableAdapter())
{
ta.Delete(userName);
}
}
public static LibraryDataSet.UsersDataTable GetDataFirstByGivenUser(string GivenUserName)
{
using(UsersTableAdapter ta=new UsersTableAdapter())
{
return ta.GetDataFirstByGivenUserName(GivenUserName);
}
}
}
}
ユーザ変更のコード部分(~/Admin/ModifyUser.aspx.cs)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ModifyUser.aspx.cs" Inherits="Library.Admin.ModifyUser"
MasterPageFile="~/Admins.Master" StylesheetTheme="Standard" %>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="UserName" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1" OnRowUpdated="GridView1_RowUpdated" AllowPaging="True"
AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="UserName" HeaderText="ユーザ名" ReadOnly="True" SortExpression="UserName" />
<asp:BoundField DataField="Information" HeaderText="ユーザ情報" SortExpression="Information" />
<asp:CheckBoxField DataField="AdminRoleFlag" HeaderText="管理者権限"
SortExpression="AdminRoleFlag" />
<asp:CheckBoxField DataField="UserRoleFlag" HeaderText="ユーザ権限"
SortExpression="UserRoleFlag" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllData"
TypeName="Library.DAC.UserInfo" UpdateMethod="Update">
<UpdateParameters>
<asp:Parameter Name="userName" Type="String" />
<asp:Parameter Name="information" Type="String" />
<asp:Parameter Name="adminRoleFlag" Type="Boolean" />
<asp:Parameter Name="userRoleFlag" Type="Boolean" />
</UpdateParameters>
</asp:ObjectDataSource>
</asp:Content>
最後に、コードビハインド
using System;
using System.Web.Security;
namespace Library.Admin
{
public partial class ModifyUser : LibPage
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
}
protected void GridView1_RowUpdated(object sender, System.Web.UI.WebControls.GridViewUpdatedEventArgs e)
{
Label1.Text = "更新しました";
}
}
}
0 件のコメント:
コメントを投稿