using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
///
///
///
public class App : DataAccess.App
{
///
/// 根据角色ID指派部门
///
///
///
public override IEnumerable RetrievesByRoleId(string roleId)
{
var apps = DictHelper.RetrieveApps().Where(d => d.Key.CompareTo("0") > 0).Select(d => new DataAccess.App()
{
Id = d.Key,
AppName = d.Value
}).ToList();
var role = RoleHelper.Retrieves().Cast().FirstOrDefault(r => r.Id == roleId);
apps.ForEach(p => p.Checked = (role != null && (role.Apps.Contains(p.Id)) || role.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)) ? "checked" : "");
return apps;
}
///
/// 根据角色ID以及选定的App ID,保到角色应用表
///
///
///
///
public override bool SaveByRoleId(string roleId, IEnumerable appIds)
{
if (string.IsNullOrEmpty(roleId)) throw new ArgumentNullException(nameof(roleId));
if (appIds == null) appIds = new string[0];
var ret = DbManager.Roles.UpdateOne(md => md.Id == roleId, Builders.Update.Set(md => md.Apps, appIds));
return true;
}
///
/// 根据指定用户名获得授权应用程序集合
///
///
///
public override IEnumerable RetrievesByUserName(string userName)
{
var ret = new List();
var roles = RoleHelper.RetrievesByUserName(userName);
if (roles.Contains("Administrators", StringComparer.OrdinalIgnoreCase))
{
ret.AddRange(DictHelper.RetrieveApps().Select(kv => kv.Key));
}
else
{
RoleHelper.Retrieves().Cast().Where(r => roles.Any(rn => rn == r.RoleName)).ToList().ForEach(r => ret.AddRange(r.Apps));
}
return ret;
}
}
}