narsil/Tanshu.Accounts.BI/SecurityBI.cs
2010-03-02 23:26:21 +05:30

98 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Security.Principal;
using System.Globalization;
using System.Threading;
using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.BI
{
public class AccountsIdentity : IIdentity
{
// the authentication type for us is always database
private static string AuthenticationTypeString = "Database";
// hash table with all the user info we have
private UserBO userInfo;
// create the user identity; all user information is in the
// hashtable passed along
private AccountsIdentity(UserBO UserInfo)
{
this.userInfo = UserInfo;
}
//create a user identity and return it to the caller
public static AccountsIdentity CreateUserIdentity(UserBO UserInfo)
{
return new AccountsIdentity(UserInfo);
}
// returns the name of the identity
public string Name { get { return UserInfo.Name; } }
// returns the userID of the identity
public UserBO UserInfo { get { return userInfo; } }
// returns whether or not identity is authenticated
public bool IsAuthenticated { get { return true; } }
// the type of authentication
public string AuthenticationType { get { return AuthenticationTypeString; } }
}
public class AccountsPrincipal : IPrincipal
{
// stores the list of roles user has
private string[] userRoles;
// the user identity we create and associate with this principal
private AccountsIdentity userIdentity;
// constructor: stores role and permission info and creates
// custom identity
private AccountsPrincipal(string[] userRoles, UserBO userInfo)
{
this.userRoles = userRoles;
// creates the IIdentity for the user and associates it with
// this IPrincipal
userIdentity = AccountsIdentity.CreateUserIdentity(userInfo);
}
// create the security principal and return it to the caller
public static AccountsPrincipal CreateAccountsPrincipal(string[] userRoles, UserBO userInfo)
{
return new AccountsPrincipal(userRoles, userInfo);
}
// returns the Identity object associated with the principal
public IIdentity Identity
{
get
{
return userIdentity;
}
}
// checks whether user belongs to role
public bool IsInRole(string Role)
{
//return userRoles.Where(ur => ur.ToLower() == Role.ToLower()).Count() > 1;
return (userRoles.Where(ur => ur.Trim().ToLower() == Role.Trim().ToLower()).Count() > 0 ? true : false);
}
}
public static class CurrentUser
{
public static UserBO user
{
get
{
AccountsIdentity identity = (AccountsIdentity)Thread.CurrentPrincipal.Identity;
return identity.UserInfo;
}
}
}
}