OpenAuth.Net/OpenAuth.Repository/RoleRepository.cs

58 lines
1.7 KiB
C#
Raw Normal View History

using System;
2015-11-19 21:49:39 +08:00
using System.Collections.Generic;
using System.Linq;
2017-04-15 22:43:21 +08:00
using System.Text;
using System.Threading.Tasks;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
2015-11-19 21:49:39 +08:00
namespace OpenAuth.Repository
{
2017-04-15 22:43:21 +08:00
public class RoleRepository :BaseRepository<Role>, IRoleRepository
2015-11-19 21:49:39 +08:00
{
2017-04-15 22:43:21 +08:00
public IEnumerable<Role> LoadRoles(int pageindex, int pagesize)
{
return Context.Roles.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
}
2017-10-11 16:19:34 +08:00
public int GetRoleCntInOrgs(params string[] orgIds)
2017-04-15 22:43:21 +08:00
{
return LoadInOrgs(orgIds).Count();
}
2017-10-11 16:19:34 +08:00
public IEnumerable<Role> LoadInOrgs(int pageindex, int pagesize, params string[] orgIds)
2015-11-19 21:49:39 +08:00
{
return LoadInOrgs(orgIds).OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
2015-11-19 21:49:39 +08:00
}
2017-10-11 16:19:34 +08:00
public void Delete(string id)
2015-11-19 21:49:39 +08:00
{
2017-04-15 22:43:21 +08:00
Delete(u =>u.Id == id);
2015-11-19 21:49:39 +08:00
}
2017-10-11 16:19:34 +08:00
public IEnumerable<Role> LoadInOrgs(params string[] orgId)
2015-11-19 21:49:39 +08:00
{
2017-04-15 22:43:21 +08:00
var roles = Context.Relevances.Where(u => u.Key == "RoleOrg"
&& orgId.Contains(u.SecondId)).Select(u => u.FirstId); //机构关联的角色
2017-04-15 22:43:21 +08:00
var result = from role in Context.Roles.Where(u =>roles.Contains(u.Id)) select role;
return result;
2015-11-19 21:49:39 +08:00
}
2017-10-11 16:19:34 +08:00
public IEnumerable<Role> LoadForUser(string userId)
2015-11-19 21:49:39 +08:00
{
2017-04-15 22:43:21 +08:00
2017-10-11 16:19:34 +08:00
if (userId == string.Empty)
2017-04-15 22:43:21 +08:00
return Find(null);
var userRoleIds =
Context.Relevances.Where(u => u.FirstId == userId && u.Key == "UserRole")
.Select(u => u.SecondId).ToList();
return Find(u => userRoleIds.Contains(u.Id));
2015-11-19 21:49:39 +08:00
}
}
2017-04-15 22:43:21 +08:00
}