Files
lserp_cs_5.0/插件库/Zhaizj.Framework/Common/Security/Utils/SecurityString.cs
T
2026-07-10 15:25:05 +08:00

105 lines
3.4 KiB
C#

using System;
using System.Collections;
using System.Text;
using Zhaizj.Framework.ORM;
namespace Zhaizj.Framework.Common.Security {
/// <summary>
/// 权限序列化后工具,格式:Zhaizj.Framework.Security.Domain.SiteRole:2:1,2,8,16,17/
/// </summary>
public class SecurityString {
public static char roleSeperator = '/';
public static char itemSeperator = ':';
public static char actionIdSeperator = ',';
public Type RoleType { get; set; }
public String TypeFullName { get; set; }
public int RoleId { get; set; }
public int[] ActionIds { get; set; }
public SecurityString( String typeFullName, int roleId, IList actions ) {
Type roleType = Entity.GetType( typeFullName );
if (roleType == null || !rft.IsInterface( roleType, typeof( IRole ) )) return;
if (roleId < 0) return;
if (actions == null) return;
int[] aids = new int[actions.Count];
for (int i = 0; i < actions.Count; i++) {
aids[i] = ((ISecurityAction)actions[i]).Id;
}
this.RoleType = roleType;
this.TypeFullName = typeFullName;
this.RoleId = roleId;
this.ActionIds = aids;
}
public SecurityString( String strOne ) {
parse( strOne );
}
private void parse( String strOne ) {
if (strUtil.IsNullOrEmpty( strOne )) return;
strOne = strOne.TrimEnd( roleSeperator );
String[] arrItem = strOne.Split( itemSeperator );
if (arrItem.Length != 3) return;
String typeFullName = arrItem[0].Trim();
Type roleType = Entity.GetType( typeFullName );
if (roleType == null || !rft.IsInterface( roleType, typeof( IRole ) )) return;
int roleId = cvt.ToInt( arrItem[1].Trim() );
if (roleId < 0) return;
int[] Ids = cvt.ToIntArray( arrItem[2].Trim().Replace( '_', ',' ) );
foreach (int intOne in Ids) {
if (intOne <= 0) return;
}
this.RoleType = roleType;
this.TypeFullName = typeFullName;
this.RoleId = roleId;
this.ActionIds = Ids;
}
public override String ToString() {
if (strUtil.IsNullOrEmpty( this.TypeFullName )) return "";
if (this.RoleId < 0) return "";
if (this.ActionIds == null) return "";
return String.Format( "{0}:{1}:{2}", this.TypeFullName, this.RoleId, cvt.ToString( ActionIds ).Replace( ',', '_' ) );
}
public String GetKey() {
return GetRoleKey( this.TypeFullName, this.RoleId );
}
public static String GetRoleKey( String typeFullName, int roleId ) {
return String.Format( "{0}_{1}", typeFullName, roleId );
}
public Boolean IsError() {
return (this.RoleType == null);
}
public IList GetActions( IList actionAll ) {
IList results = new ArrayList();
foreach (ISecurityAction action in actionAll) {
foreach (int actionId in ActionIds) {
if (action.Id == actionId) {
results.Add( action );
continue;
}
}
}
return results;
}
}
}