170 lines
5.5 KiB
C#
170 lines
5.5 KiB
C#
using Cowain.Bake.BLL;
|
|
using Cowain.Bake.Common;
|
|
using Prism.Regions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Unity;
|
|
using Cowain.Bake.Model;
|
|
using Prism.Ioc;
|
|
using Cowain.Bake.Model.Entity;
|
|
using Prism.Commands;
|
|
using HandyControl.Controls;
|
|
|
|
namespace Cowain.Bake.UI.UserManagerment.ViewModels
|
|
{
|
|
public class AuthorityManagementViewModel : ViewModelBase, INotifyPropertyChanged
|
|
{
|
|
public AuthorityManagementViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
|
|
{
|
|
this.PageTitle = "权限管理";
|
|
_unityContainer = unityContainer;
|
|
RoleList = userService.GetAllRole();
|
|
InitTreeView();
|
|
SelectedRole= RoleList.FirstOrDefault();
|
|
RefreshAuthority();
|
|
}
|
|
#region 绑定属性
|
|
UserService userService = MyAppContainer.Current.Resolve<UserService>();
|
|
|
|
public new event PropertyChangedEventHandler PropertyChanged;
|
|
public void OnPropertyChange(string propName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
|
|
}
|
|
|
|
//用于展示角色的listbox
|
|
private List<TRoleInfo> roleList;
|
|
|
|
public List<TRoleInfo> RoleList
|
|
{
|
|
get { return roleList; }
|
|
set { roleList = value; }
|
|
}
|
|
|
|
//用于权限展示的treeview
|
|
private ObservableCollection<AuthorityEntity> authorityList;
|
|
|
|
public ObservableCollection<AuthorityEntity> AuthorityList
|
|
{
|
|
get { return authorityList; }
|
|
set { authorityList = value; }
|
|
}
|
|
|
|
//选中的角色
|
|
private TRoleInfo selectedRole;
|
|
|
|
public TRoleInfo SelectedRole
|
|
{
|
|
get { return selectedRole; }
|
|
set
|
|
{
|
|
if (selectedRole != value)
|
|
{
|
|
selectedRole = value;
|
|
OnPropertyChange(nameof(SelectedRole));
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void InitTreeView()
|
|
{
|
|
AuthorityList = new ObservableCollection<AuthorityEntity>();
|
|
var menuList = userService.GetAllMenuInfo();
|
|
//第一个循环,循环父节点
|
|
for (int i = 1; i <= menuList.Where(x=>x.ParentId==0).ToList().Count; i++)
|
|
{
|
|
var a = menuList.Where(p => p.Id == i).FirstOrDefault();
|
|
AuthorityEntity parentItem = new AuthorityEntity
|
|
{
|
|
Header = a.Header,
|
|
HeaderName = a.HeaderName,
|
|
TargetView = a.TargetView,
|
|
MenuId = a.Id,
|
|
ParentId = a.ParentId,
|
|
CheckboxVisibility = System.Windows.Visibility.Collapsed
|
|
};
|
|
|
|
//第二个循环,循环子节点
|
|
foreach(var item in menuList.Where(c => c.ParentId == i).ToList())
|
|
{
|
|
|
|
|
|
AuthorityEntity childItem = new AuthorityEntity
|
|
{
|
|
Header = item.Header,
|
|
HeaderName = item.HeaderName,
|
|
TargetView = item.TargetView,
|
|
MenuId = item.Id,
|
|
ParentId = item.ParentId,
|
|
|
|
CheckboxVisibility = System.Windows.Visibility.Visible
|
|
|
|
};
|
|
parentItem.Children.Add(childItem);
|
|
}
|
|
|
|
AuthorityList.Add(parentItem);
|
|
}
|
|
|
|
}
|
|
|
|
public DelegateCommand SaveCommand => new DelegateCommand(() =>
|
|
{
|
|
StringBuilder authorityString = new StringBuilder();
|
|
foreach (var parentItem in AuthorityList)
|
|
{
|
|
foreach(var childitem in parentItem.Children)
|
|
{
|
|
if (childitem.IsHasAuthority)
|
|
{
|
|
if (!string.IsNullOrEmpty(childitem.HeaderName))
|
|
{
|
|
authorityString.Append(childitem.HeaderName);
|
|
authorityString.Append(";");
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
SelectedRole.AccessNode= authorityString.ToString();
|
|
_unityContainer.Resolve<RoleInfoService>().Update(SelectedRole);
|
|
Growl.Success("保存成功!");
|
|
});
|
|
|
|
public DelegateCommand ListBoxSelectionChangedCommand => new DelegateCommand(() =>
|
|
{
|
|
RefreshAuthority();
|
|
});
|
|
|
|
public void RefreshAuthority()
|
|
{
|
|
string authority = userService.GetAuthority(selectedRole.RoleName);
|
|
foreach (var parent in AuthorityList)
|
|
{
|
|
foreach (var child in parent.Children)
|
|
{
|
|
if (string.IsNullOrEmpty(child.HeaderName))
|
|
{
|
|
child.IsHasAuthority = true;
|
|
}
|
|
else
|
|
{
|
|
child.IsHasAuthority = authority.Contains(child.HeaderName) ? true : false;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|