ASP.NET&Spring.NET&NHibernate最佳实践(十六)——第4章权限子系统(9)
角色服务接口(IRoleService.cs)
角色服务类(RoleService.cs)
using System;
using Guushuuse.SalaryPrj.Security.DomainModel;
using Guushuuse.SalaryPrj.Security.Dao;
namespace Guushuuse.SalaryPrj.Security.Service
{
/// <summary>
/// 角色服务接口
/// </summary>
public interface IRoleService
{
int AddUsersToRoles(string applicationName, string[] usernames, string[] roleNames);
int CreateRole(string applicationName, string roleName);
int DeleteRole(string applicationName, string roleName, bool deleteOnlyIfRoleIsEmpty);
string[] FindUsersInRole(string applicationName, string roleName, string usernameToMatch);
string[] GetAllRoles(string applicationName);
Role GetRole(int roleID);
string[] GetRolesForUser(string applicationName, string username);
string[] GetUsersInRole(string applicationName, string roleName);
bool IsUserInRole(string applicationName, string username, string roleName);
int RemoveUsersFromRoles(string applicationName, string[] usernames, string[] roleNames);
IRoleDao RoleDao { get; set; }
bool RoleExists(string applicationName, string roleName);
IUserDao UserDao { get; set; }
}
}
using Guushuuse.SalaryPrj.Security.DomainModel;
using Guushuuse.SalaryPrj.Security.Dao;
namespace Guushuuse.SalaryPrj.Security.Service
{
/// <summary>
/// 角色服务接口
/// </summary>
public interface IRoleService
{
int AddUsersToRoles(string applicationName, string[] usernames, string[] roleNames);
int CreateRole(string applicationName, string roleName);
int DeleteRole(string applicationName, string roleName, bool deleteOnlyIfRoleIsEmpty);
string[] FindUsersInRole(string applicationName, string roleName, string usernameToMatch);
string[] GetAllRoles(string applicationName);
Role GetRole(int roleID);
string[] GetRolesForUser(string applicationName, string username);
string[] GetUsersInRole(string applicationName, string roleName);
bool IsUserInRole(string applicationName, string username, string roleName);
int RemoveUsersFromRoles(string applicationName, string[] usernames, string[] roleNames);
IRoleDao RoleDao { get; set; }
bool RoleExists(string applicationName, string roleName);
IUserDao UserDao { get; set; }
}
}
角色服务类(RoleService.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Guushuuse.SalaryPrj.Security.Dao;
using Guushuuse.SalaryPrj.Security.DomainModel;
using Spring.Transaction.Interceptor;
using System.Collections;
namespace Guushuuse.SalaryPrj.Security.Service
{
/// <summary>
/// 角色服务类
/// </summary>
public class RoleService : IRoleService
{
private IRoleDao _roleDao;
private IUserDao _userDao;
public IRoleDao RoleDao
{
get { return _roleDao; }
set { _roleDao = value; }
}
public IUserDao UserDao
{
get { return _userDao; }
set { _userDao = value; }
}
[Transaction(ReadOnly = false)]
public int CreateRole(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
if (RoleExists(applicationName, roleName))
{
return 5;
}
Role role = new Role();
role.Name = roleName;
role.Application = application;
_roleDao.CreateRole(role);
return 0;
}
[Transaction(ReadOnly = false)]
public int DeleteRole(string applicationName, string roleName, bool deleteOnlyIfRoleIsEmpty)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
if (deleteOnlyIfRoleIsEmpty)
{
if (_userDao.GetUsersCountInRole(application, role) > 0)
{
return 4;
}
}
_roleDao.DeleteRole(role);
return 0;
}
public bool RoleExists(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role != null)
{
return true;
}
else
{
return false;
}
}
public string[] FindUsersInRole(string applicationName, string roleName, string usernameToMatch)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return null;
}
IList userList = _userDao.FindUsersInRole(application, role, usernameToMatch);
if (userList.Count == 0)
{
return null;
}
string[] usernames = new String[userList.Count];
int i = 0;
foreach (User user in userList)
{
usernames[i++] = user.Username;
}
return usernames;
}
public string[] GetAllRoles(string applicationName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList roleList = _roleDao.GetRoles(application);
if (roleList.Count == 0)
{
return null;
}
string[] roleNames = new String[roleList.Count];
int i = 0;
foreach (Role role in roleList)
{
roleNames[i++] = role.Name;
}
return roleNames;
}
public string[] GetRolesForUser(string applicationName, string username)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
User user = _userDao.GetUser(application, username);
if (user == null)
{
return null;
}
string[] roleNames = new String[user.Roles.Count];
int i = 0;
foreach (Role role in user.Roles)
{
roleNames[i++] = role.Name;
}
return roleNames;
}
public string[] GetUsersInRole(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return null;
}
IList userList = _userDao.GetUsersInRole(application, role);
if (userList.Count == 0)
{
return null;
}
string[] usernames = new String[userList.Count];
int i = 0;
foreach (User user in userList)
{
usernames[i++] = user.Username;
}
return usernames;
}
public bool IsUserInRole(string applicationName, string username, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
User user = _userDao.GetUser(application, username);
if (user == null)
{
return false;
}
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return false;
}
if (user.Roles.Contains(role))
{
return true;
}
else
{
return false;
}
}
[Transaction(ReadOnly = false)]
public int AddUsersToRoles(string applicationName, string[] usernames, string[] roleNames)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList users = new ArrayList();
IList roles = new ArrayList();
foreach (string username in usernames)
{
User user = _userDao.GetUser(application, username);
if (user == null)
{
return 1;
}
users.Add(user);
}
foreach (string roleName in roleNames)
{
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
roles.Add(role);
}
foreach (User user in users)
{
foreach (Role role in roles)
{
if (!user.Roles.Contains(role))
{
user.Roles.Add(role);
_userDao.UpdateUser(user);
}
}
}
return 0;
}
[Transaction(ReadOnly = false)]
public int RemoveUsersFromRoles(string applicationName, string[] usernames, string[] roleNames)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList users = new ArrayList();
IList roles = new ArrayList();
foreach (string username in usernames)
{
User user = _userDao.GetUser(application, username);
if (user == null)
{
return 1;
}
users.Add(user);
}
foreach (string roleName in roleNames)
{
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
roles.Add(role);
}
foreach (User user in users)
{
foreach (Role role in roles)
{
if (user.Roles.Contains(role))
{
user.Roles.Remove(role);
_userDao.UpdateUser(user);
}
}
}
return 0;
}
public Role GetRole(int roleID)
{
return _roleDao.GetRole(roleID);
}
}
}
using System.Collections.Generic;
using System.Text;
using Guushuuse.SalaryPrj.Security.Dao;
using Guushuuse.SalaryPrj.Security.DomainModel;
using Spring.Transaction.Interceptor;
using System.Collections;
namespace Guushuuse.SalaryPrj.Security.Service
{
/// <summary>
/// 角色服务类
/// </summary>
public class RoleService : IRoleService
{
private IRoleDao _roleDao;
private IUserDao _userDao;
public IRoleDao RoleDao
{
get { return _roleDao; }
set { _roleDao = value; }
}
public IUserDao UserDao
{
get { return _userDao; }
set { _userDao = value; }
}
[Transaction(ReadOnly = false)]
public int CreateRole(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
if (RoleExists(applicationName, roleName))
{
return 5;
}
Role role = new Role();
role.Name = roleName;
role.Application = application;
_roleDao.CreateRole(role);
return 0;
}
[Transaction(ReadOnly = false)]
public int DeleteRole(string applicationName, string roleName, bool deleteOnlyIfRoleIsEmpty)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
if (deleteOnlyIfRoleIsEmpty)
{
if (_userDao.GetUsersCountInRole(application, role) > 0)
{
return 4;
}
}
_roleDao.DeleteRole(role);
return 0;
}
public bool RoleExists(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role != null)
{
return true;
}
else
{
return false;
}
}
public string[] FindUsersInRole(string applicationName, string roleName, string usernameToMatch)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return null;
}
IList userList = _userDao.FindUsersInRole(application, role, usernameToMatch);
if (userList.Count == 0)
{
return null;
}
string[] usernames = new String[userList.Count];
int i = 0;
foreach (User user in userList)
{
usernames[i++] = user.Username;
}
return usernames;
}
public string[] GetAllRoles(string applicationName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList roleList = _roleDao.GetRoles(application);
if (roleList.Count == 0)
{
return null;
}
string[] roleNames = new String[roleList.Count];
int i = 0;
foreach (Role role in roleList)
{
roleNames[i++] = role.Name;
}
return roleNames;
}
public string[] GetRolesForUser(string applicationName, string username)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
User user = _userDao.GetUser(application, username);
if (user == null)
{
return null;
}
string[] roleNames = new String[user.Roles.Count];
int i = 0;
foreach (Role role in user.Roles)
{
roleNames[i++] = role.Name;
}
return roleNames;
}
public string[] GetUsersInRole(string applicationName, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return null;
}
IList userList = _userDao.GetUsersInRole(application, role);
if (userList.Count == 0)
{
return null;
}
string[] usernames = new String[userList.Count];
int i = 0;
foreach (User user in userList)
{
usernames[i++] = user.Username;
}
return usernames;
}
public bool IsUserInRole(string applicationName, string username, string roleName)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
User user = _userDao.GetUser(application, username);
if (user == null)
{
return false;
}
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return false;
}
if (user.Roles.Contains(role))
{
return true;
}
else
{
return false;
}
}
[Transaction(ReadOnly = false)]
public int AddUsersToRoles(string applicationName, string[] usernames, string[] roleNames)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList users = new ArrayList();
IList roles = new ArrayList();
foreach (string username in usernames)
{
User user = _userDao.GetUser(application, username);
if (user == null)
{
return 1;
}
users.Add(user);
}
foreach (string roleName in roleNames)
{
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
roles.Add(role);
}
foreach (User user in users)
{
foreach (Role role in roles)
{
if (!user.Roles.Contains(role))
{
user.Roles.Add(role);
_userDao.UpdateUser(user);
}
}
}
return 0;
}
[Transaction(ReadOnly = false)]
public int RemoveUsersFromRoles(string applicationName, string[] usernames, string[] roleNames)
{
Application application = ServiceLocator.ApplicationService.GetApplication(applicationName);
IList users = new ArrayList();
IList roles = new ArrayList();
foreach (string username in usernames)
{
User user = _userDao.GetUser(application, username);
if (user == null)
{
return 1;
}
users.Add(user);
}
foreach (string roleName in roleNames)
{
Role role = _roleDao.GetRole(application, roleName);
if (role == null)
{
return 2;
}
roles.Add(role);
}
foreach (User user in users)
{
foreach (Role role in roles)
{
if (user.Roles.Contains(role))
{
user.Roles.Remove(role);
_userDao.UpdateUser(user);
}
}
}
return 0;
}
public Role GetRole(int roleID)
{
return _roleDao.GetRole(roleID);
}
}
}