/
QueueSASPermissions.ts
106 lines (98 loc) · 2.84 KB
/
QueueSASPermissions.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* ONLY AVAILABLE IN NODE.JS RUNTIME.
*
* This is a helper class to construct a string representing the permissions granted by a ServiceSAS to a Queue. Setting
* a value to true means that any SAS which uses these permissions will grant permissions for that operation. Once all
* the values are set, this should be serialized with toString and set as the permissions field on a
* {@link QueueSASSignatureValues} object. It is possible to construct the permissions string without this class, but
* the order of the permissions is particular and this class guarantees correctness.
*
* @export
* @class QueueSASPermissions
*/
export class QueueSASPermissions {
/**
* Creates a {@link QueueSASPermissions} from the specified permissions string. This method will throw an
* Error if it encounters a character that does not correspond to a valid permission.
*
* @static
* @param {string} permissions
* @returns {QueueSASPermissions}
* @memberof QueueSASPermissions
*/
public static parse(permissions: string): QueueSASPermissions {
const queueSASPermissions = new QueueSASPermissions();
for (const char of permissions) {
switch (char) {
case "r":
queueSASPermissions.read = true;
break;
case "a":
queueSASPermissions.add = true;
break;
case "u":
queueSASPermissions.update = true;
break;
case "p":
queueSASPermissions.process = true;
break;
default:
throw new RangeError(`Invalid permission: ${char}`);
}
}
return queueSASPermissions;
}
/**
* Specifies Read access granted.
*
* @type {boolean}
* @memberof QueueSASPermissions
*/
public read: boolean = false;
/**
* Specifies Add access granted.
*
* @type {boolean}
* @memberof QueueSASPermissions
*/
public add: boolean = false;
/**
* Specifies Update access granted.
*
* @type {boolean}
* @memberof QueueSASPermissions
*/
public update: boolean = false;
/**
* Specifies Process access granted.
*
* @type {boolean}
* @memberof QueueSASPermissions
*/
public process: boolean = false;
/**
* Converts the given permissions to a string. Using this method will guarantee the permissions are in an
* order accepted by the service.
*
* @returns {string} A string which represents the QueueSASPermissions
* @memberof QueueSASPermissions
*/
public toString(): string {
const permissions: string[] = [];
if (this.read) {
permissions.push("r");
}
if (this.add) {
permissions.push("a");
}
if (this.update) {
permissions.push("u");
}
if (this.process) {
permissions.push("p");
}
return permissions.join("");
}
}