using Bootstrap.Security.DataAccess;
using Longbow.Data;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
///
///
///
[TableName("Roles")]
public class Role
{
///
/// 获得/设置 角色主键ID
///
public string Id { get; set; }
///
/// 获得/设置 角色名称
///
public string RoleName { get; set; }
///
/// 获得/设置 角色描述
///
public string Description { get; set; }
///
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
///
[ResultColumn]
public string Checked { get; set; }
///
/// 查询所有角色
///
///
public virtual IEnumerable Retrieves() => DbManager.Create().Fetch();
///
/// 保存用户角色关系
///
///
///
///
public virtual bool SaveByUserId(string userId, IEnumerable roleIds)
{
var ret = false;
var db = DbManager.Create();
try
{
db.BeginTransaction();
// delete user from config table
db.Execute("delete from UserRole where UserID = @0", userId);
db.InsertBatch("UserRole", roleIds.Select(g => new { UserID = userId, RoleID = g }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 查询某个用户所拥有的角色
///
///
public virtual IEnumerable RetrievesByUserId(string userId) => DbManager.Create().Fetch("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @0", userId);
///
/// 删除角色表
///
///
public virtual bool Delete(IEnumerable value)
{
if (!value.Any()) return true;
bool ret = false;
var ids = string.Join(",", value);
var db = DbManager.Create();
try
{
db.BeginTransaction();
db.Execute($"delete from UserRole where RoleID in ({ids})");
db.Execute($"delete from RoleGroup where RoleID in ({ids})");
db.Execute($"delete from NavigationRole where RoleID in ({ids})");
db.Delete($"where ID in ({ids})");
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 保存新建/更新的角色信息
///
///
///
public virtual bool Save(Role p)
{
if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
DbManager.Create().Save(p);
return true;
}
///
/// 查询某个菜单所拥有的角色
///
///
///
public virtual IEnumerable RetrievesByMenuId(string menuId) => DbManager.Create().Fetch("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @0", menuId);
///
///
///
///
///
///
public virtual bool SavaByMenuId(string menuId, IEnumerable roleIds)
{
var ret = false;
var db = DbManager.Create();
db.BeginTransaction();
try
{
// delete role from config table
db.Execute("delete from NavigationRole where NavigationID = @0", menuId);
db.InsertBatch("NavigationRole", roleIds.Select(g => new { NavigationID = menuId, RoleID = g }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 根据GroupId查询和该Group有关的所有Roles
///
///
///
public virtual IEnumerable RetrievesByGroupId(string groupId) => DbManager.Create().Fetch("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @0", groupId);
///
/// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息
///
///
///
///
public virtual bool SaveByGroupId(string groupId, IEnumerable roleIds)
{
var ret = false;
var db = DbManager.Create();
try
{
// delete user from config table
db.Execute("delete from RoleGroup where GroupID = @0", groupId);
db.InsertBatch("RoleGroup", roleIds.Select(g => new { GroupID = groupId, RoleID = g }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
///
///
///
///
public virtual IEnumerable RetrievesByUserName(string userName) => DbHelper.RetrieveRolesByUserName(userName);
///
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators -> GroupNavigatorRole -> Role查查询某个用户所拥有的角色
///
///
public virtual IEnumerable RetrievesByUrl(string url) => DbHelper.RetrieveRolesByUrl(url);
}
}