-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathTsPermissionHelper.cs
146 lines (122 loc) · 4.16 KB
/
TsPermissionHelper.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
// TSLib - A free TeamSpeak 3 and 5 client library
// Copyright (C) 2017 TSLib contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Open Software License v. 3.0
//
// You should have received a copy of the Open Software License along with this
// program. If not, see <https://opensource.org/licenses/OSL-3.0>.
using System.Linq;
using TSLib.Commands;
using TSLib.Helper;
using TSLib.Messages;
namespace TSLib
{
public static partial class TsPermissionHelper
{
public static ICommandPart GetAsParameter(IPermissionTransform permissionTransform, TsPermission permission)
{
if (permissionTransform is null || permissionTransform == DummyPermissionTransform.Instance)
return new CommandParameter("permsid", permission.ToString());
else
return new CommandParameter("permid", permissionTransform.GetId(permission));
}
public static ICommandPart GetAsMultiParameter(IPermissionTransform permissionTransform, params TsPermission[] permission)
{
if (permissionTransform is null || permissionTransform == DummyPermissionTransform.Instance)
return new CommandMultiParameter("permsid", permission.Select(x => x.ToString()));
else
return new CommandMultiParameter("permid", permission.Select(x => permissionTransform.GetId(x)));
}
public static PermOverview Combine(this PermOverview perm, PermOverview other)
{
// A pretty good documentation on permissions: http://yat.qa/ressourcen/definitionen-und-algorithmen/#permissions
switch (perm.PermissionType)
{
case PermissionType.ServerGroup:
switch (other.PermissionType)
{
case PermissionType.ServerGroup:
if (perm.PermissionNegated && other.PermissionNegated)
return perm.PermissionValue < other.PermissionValue ? perm : other;
else if (other.PermissionNegated)
return other;
else
return perm.PermissionValue > other.PermissionValue ? perm : other;
case PermissionType.GlobalClient:
case PermissionType.ChannelClient:
return other;
case PermissionType.Channel:
case PermissionType.ChannelGroup:
if (perm.PermissionSkip)
return perm;
else
return other;
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
case PermissionType.GlobalClient:
switch (other.PermissionType)
{
case PermissionType.ServerGroup:
return Combine(other, perm);
case PermissionType.GlobalClient:
return perm.PermissionValue > other.PermissionValue ? perm : other;
case PermissionType.Channel:
case PermissionType.ChannelGroup:
if (perm.PermissionSkip)
return perm;
else
return other;
case PermissionType.ChannelClient:
return other;
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
case PermissionType.Channel:
switch (other.PermissionType)
{
case PermissionType.ServerGroup:
case PermissionType.GlobalClient:
return Combine(other, perm);
case PermissionType.Channel:
return perm.PermissionValue > other.PermissionValue ? perm : other;
case PermissionType.ChannelGroup:
case PermissionType.ChannelClient:
return other;
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
case PermissionType.ChannelGroup:
switch (other.PermissionType)
{
case PermissionType.ServerGroup:
case PermissionType.GlobalClient:
case PermissionType.Channel:
return perm;
case PermissionType.ChannelGroup:
return perm.PermissionValue > other.PermissionValue ? perm : other;
case PermissionType.ChannelClient:
return other;
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
case PermissionType.ChannelClient:
switch (other.PermissionType)
{
case PermissionType.ServerGroup:
case PermissionType.GlobalClient:
case PermissionType.Channel:
case PermissionType.ChannelGroup:
return perm;
case PermissionType.ChannelClient:
return perm.PermissionValue > other.PermissionValue ? perm : other;
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
default:
throw Tools.UnhandledDefault(perm.PermissionType);
}
}
}
}