私は通常、プロジェクトで次のようなコードを使用します。
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
しかし、データベースでこのボタンを見ることができるロールを制御したいと思います。
この目的のために、データベース設計はどうあるべきか?
ありがとう。
私は通常、プロジェクトで次のようなコードを使用します。
If user.IsInRole("Admin") Then
deleteButton.Visible = True
else
deleteButton.Visible = False
しかし、データベースでこのボタンを見ることができるロールを制御したいと思います。
この目的のために、データベース設計はどうあるべきか?
ありがとう。
好きなようにデザインを作成しますが、ASP.NET 側では独自の MembershipProvider を実装します。これにより、DB 設計が .NET で使用できるユーザー/ロールに変換されます。その後、通常どおり使用できますuser.isInRole("Admin")
-with :)
LDAP は、承認と認証に最適なオプションです。同じ目的でopenLDAP APIを使用できます。
さて、1つの設計は次のようなテーブルを持つことです:
User(UserID, ...) PK = UserID
Role(RoleID, RoleName, ...) PK = RoleID
UserHasRole(UserHasRoleID, UserID, RoleID) PK=UserHasRoleID ; Unique= (UserID, RoleID)
それが一つの方法です。これは、任意のオブジェクトベースの承認システムではなく、役割ベースのシステムです(任意のシステムでは、各オブジェクトにアクセス許可を設定します。たとえば、このユーザーxには顧客のDELETEアクセス許可などがあります)。
もっと明確にすべきかもしれませんが、方法がわかりません:)。もう一度やり直します。
たとえば、削除ボタンに次のコードを使用します。
if user.isInRole("Admin") then
deleteButton.visible = true
else
deleteButton.visible = false
全体として、ユーザーには「モデレーター」の役割があり、削除ボタンも表示される必要があることを決定します。したがって、コードを次のように変更する必要があります。
if user.isInRole("Admin","Moderator") then
deleteButton.visible = true
else
deleteButton.visible = false
これを制御するデータベース設計がある場合は、コードを変更する必要はありませんでした。
さて、どうあるべきですか?
コード:
public class YourSqlRoleProvider : System.Web.Security.RoleProvider
{
private string ConnectionString { get; set; }
public override void AddUsersToRoles(string[] userNames, string[] roleNames)
{
// logic here
}
public override string ApplicationName
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override void CreateRole(string roleName)
{
throw new NotSupportedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotSupportedException();
}
public override string[] FindUsersInRole(string roleName, string userNameToMatch)
{
throw new NotSupportedException();
}
public override string[] GetAllRoles()
{
// logic here
}
public override string[] GetRolesForUser(string userName)
{
// logic here
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotSupportedException();
}
public override bool IsUserInRole(string userName, string roleName)
{
return GetRolesForUser(userName).Contains(roleName);
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
this.ConnectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;
base.Initialize(name, config);
}
public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
{
throw new NotSupportedException();
}
public override bool RoleExists(string roleName)
{
throw new NotSupportedException();
}
}
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<clear />
<add name="YourConnectionString" providerName="System.Data.SqlClient" connectionString="connection string here" />
</connectionStrings>
<system.web>
<roleManager defaultProvider="YourSqlRoleProvider" enabled="true">
<providers>
<clear />
<add name="YourSqlRoleProvider" type="YourSqlRoleProvider" connectionStringName="YourConnectionString" />
</providers>
</roleManager>
</system.web>
</configuration>
.NET を使用していると仮定すると、これを行う 1 つの方法は、独自のロール プロバイダーとメンバーシップ プロバイダーを実装することです。次に、必要なアイテムを含むインターフェイスを実装することで機能を追加できます (このサンプルを頭のてっぺんから思いついたので、少し大雑把に思えたら申し訳ありません)。
public interface ICustomRole
{
bool IsInRole(string userName, object[] params roles);
}
public class MyCustomRole : RoleProvider, ICustomRole
{
public IsInRole(MembershipUser user, object[] params roles)
{
if (roles == null || roles.Length == 0)
throw new ArgumentException("roles");
// Put your logic here for accessing the roles
}
}
次に、コードで次のようにします。
bool isValid = ((ICustomRole)Roles.Provider).IsInRole(
User, new[] { "Admin", "Moderator", "Validator" });