/
PermissionCondition.cs
121 lines (108 loc) · 4.53 KB
/
PermissionCondition.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
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
using System.Diagnostics.CodeAnalysis;
namespace Realms.Sync
{
/// <summary>
/// A class describing the condition based on which permissions will be applied.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass")]
public abstract class PermissionCondition
{
/// <summary>
/// Apply permissions based on the user's Id.
/// </summary>
/// <returns>A <see cref="PermissionCondition"/> containing information about the user's Id.</returns>
/// <param name="userId">The Id of the user.</param>
public static PermissionCondition UserId(string userId)
{
return new UserIdCondition(userId);
}
/// <summary>
/// Gets a <see cref="PermissionCondition"/> that describes the default permissions for all users
/// who don't have explicit permissions applied. The <see cref="AccessLevel"/> granted alongside
/// this condition will also be used as default access level for future new users.
/// </summary>
/// <remarks>
/// The default permissions are not additive with more specific permissions, even if the latter
/// are more restrictive - for example, a user who has been granted <see cref="AccessLevel.Read"/>
/// access will not be write to a Realm, even if the default permissions grant <see cref="AccessLevel.Write"/>
/// access.
/// </remarks>
/// <value>A <see cref="PermissionCondition"/> describing the default permissions.</value>
public static PermissionCondition Default => UserId("*");
/// <summary>
/// Apply permissions based on the user's Email when using the username/password login provider.
/// </summary>
/// <returns>A <see cref="PermissionCondition"/> containing information about the user's email.</returns>
/// <param name="email">The email (username) of the user that will be affected by this condition.</param>
public static PermissionCondition Email(string email)
{
return KeyValue("email", email);
}
/// <summary>
/// Apply permissions based on a key/value combination in the user's metadata.
/// </summary>
/// <returns>
/// A <see cref="PermissionCondition"/> containing information about the key/value combination that will be used
/// for matching against.
/// </returns>
/// <param name="key">The metadata key to look for.</param>
/// <param name="value">The metadata value that must match the key.</param>
public static PermissionCondition KeyValue(string key, string value)
{
return new KeyValueCondition(key, value);
}
internal abstract object ToJsonObject();
}
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass")]
internal class UserIdCondition : PermissionCondition
{
public new string UserId { get; }
public UserIdCondition(string userId)
{
UserId = userId;
}
internal override object ToJsonObject()
{
return new
{
userId = UserId
};
}
}
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass")]
internal class KeyValueCondition : PermissionCondition
{
public string Key { get; }
public string Value { get; }
public KeyValueCondition(string key, string value)
{
Key = key;
Value = value;
}
internal override object ToJsonObject()
{
return new
{
metadataKey = Key,
metadataValue = Value,
};
}
}
}