/
DefaultFileAdapter.cs
151 lines (133 loc) · 4.68 KB
/
DefaultFileAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using NetCasbin.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetCasbin.Persist.FileAdapter
{
public class DefaultFileAdapter : IAdapter
{
protected readonly string filePath;
private readonly Boolean _readOnly = false;
private readonly StreamReader _byteArrayInputStream;
public DefaultFileAdapter(string filePath)
{
this.filePath = filePath;
}
public DefaultFileAdapter(Stream inputStream)
{
_readOnly = true;
try
{
_byteArrayInputStream = new StreamReader(inputStream);
}
catch (IOException e)
{
throw new Exception("File adapter init error");
}
}
public void LoadPolicy(Model.Model model)
{
if (filePath != null && !"".Equals(filePath))
{
using (var sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
LoadPolicyData(model, Helper.LoadPolicyLine, sr);
}
}
if (_byteArrayInputStream != null)
{
LoadPolicyData(model, Helper.LoadPolicyLine, _byteArrayInputStream);
}
}
public Task LoadPolicyAsync(Model.Model model)
{
throw new NotImplementedException();
}
public void SavePolicy(Model.Model model)
{
var policy = ConvertToPolicy(model);
SavePolicyFile(string.Join("\n", policy));
}
public async Task SavePolicyAsync(Model.Model model)
{
var policy = ConvertToPolicy(model);
await SavePolicyFileAsync(string.Join("\n", policy));
}
public void AddPolicy(string sec, string ptype, IList<string> rule)
{
throw new NotImplementedException("not implemented");
}
public Task AddPolicyAsync(string sec, string ptype, IList<string> rule)
{
throw new NotImplementedException();
}
public void RemovePolicy(string sec, string ptype, IList<string> rule)
{
throw new NotImplementedException("not implemented");
}
public void RemoveFilteredPolicy(string sec, string ptype, int fieldIndex, params string[] fieldValues)
{
throw new NotImplementedException("not implemented");
}
private List<string> GetModelPolicy(Model.Model model, string ptype)
{
List<string> policy = new List<string>();
model.Model[ptype].ToList().ForEach(item =>
{
var k = item.Key;
var v = item.Value;
List<string> p = v.Policy.Select(x => $"{k}, {Utility.ArrayToString(x)}").ToList();
policy.AddRange(p);
});
return policy;
}
private void LoadPolicyData(Model.Model model, Helper.LoadPolicyLineHandler<string, Model.Model> handler, StreamReader inputStream)
{
while (!inputStream.EndOfStream)
{
var line = inputStream.ReadLine();
handler(line, model);
}
}
private IList<string> ConvertToPolicy(Model.Model model)
{
if (_byteArrayInputStream != null && _readOnly)
{
throw new Exception("Policy file can not write, because use inputStream is readOnly");
}
if (filePath == null || "".Equals(filePath))
{
throw new Exception("invalid file path, file path cannot be empty");
}
List<string> policy = new List<string>();
policy.AddRange(GetModelPolicy(model, "p"));
if (model.Model.ContainsKey("g"))
{
policy.AddRange(GetModelPolicy(model, "g"));
}
return policy;
}
private void SavePolicyFile(string text)
{
File.WriteAllText(filePath, text, Encoding.UTF8);
}
private async Task SavePolicyFileAsync(string text)
{
text = text ?? "";
var content = Encoding.UTF8.GetBytes(text);
using (var fs = new FileStream(
path: filePath,
mode: FileMode.Create,
access: FileAccess.Write,
share: FileShare.None,
bufferSize: 4096,
useAsync: true))
{
await fs.WriteAsync(content, 0, content.Length);
}
}
}
}