-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
118 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const permissionCheckError = 'An error occurred while trying to validate Mojira permissions.\n'; | ||
|
||
/** | ||
* Parses the JSON response and checks if the currently logged-in user is in a user group other than `users`. | ||
* @param {string} response The API response, hopefully in JSON format | ||
* | ||
* @returns {boolean} whether the user is in at least one non-`users` group | ||
* @throws {string} if there's no valid JSON or the JSON does not follow the API scheme. | ||
*/ | ||
function parsePermissionResponse(response) { | ||
try { | ||
const json = JSON.parse(response); | ||
|
||
const groups = json.groups?.items; | ||
if (!groups) throw 'The API response is malformed and does not include groups.'; | ||
|
||
return groups.filter(group => group.name !== 'users').length > 0; | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
throw `${permissionCheckError}The server returned invalid JSON: ${error.message}`; | ||
} else { | ||
throw `${permissionCheckError}${error}`; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the currently logged in user is a volunteer user (moderator / helper). | ||
* | ||
* @returns {Promise<boolean>} true is the logged in user is a helper+, false otherwise. | ||
* @throws {string} if an error occurs | ||
*/ | ||
function queryPermissions() { | ||
return new Promise(async (resolve, reject) => { | ||
const httpRequest = new XMLHttpRequest(); | ||
httpRequest.onreadystatechange = async () => { | ||
if (httpRequest.readyState === XMLHttpRequest.DONE) { | ||
if (httpRequest.status === 200) { | ||
try { | ||
const perm = parsePermissionResponse(httpRequest.responseText); | ||
resolve(perm); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
} | ||
reject( | ||
`${permissionCheckError}The server returned status code ${httpRequest.status} ${httpRequest.statusText}.` | ||
); | ||
} | ||
} | ||
|
||
try { | ||
httpRequest.open('GET', 'https://bugs.mojang.com/rest/api/2/myself?expand=groups'); | ||
httpRequest.send(); | ||
} catch (error) { | ||
reject(`${permissionCheckError}${error}`); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Checks if the currently logged in user is a volunteer user (moderator / helper). | ||
* Uses the value from the cache unless the user name has changed. | ||
* | ||
* @returns {Promise<boolean>} true if the logged in user is a helper+, false otherwise. | ||
* @throws {string} if an error occurr. | ||
*/ | ||
async function validatePermissions() { | ||
const userName = document.querySelector('meta[name=ajs-remote-user]')?.attributes?.getNamedItem('content')?.value; | ||
|
||
if (!userName) { | ||
await browser.runtime.sendMessage({ id: 'set-permission-cache', user: undefined, value: false }); | ||
return false; | ||
} | ||
|
||
const permissionCache = await browser.runtime.sendMessage({ id: 'get-permission-cache' }); | ||
if (permissionCache.user === userName) { | ||
return permissionCache.value; | ||
} | ||
|
||
const userPermissions = queryPermissions(); | ||
await browser.runtime.sendMessage({ id: 'set-permission-cache', user: userName, value: userPermissions }); | ||
return userPermissions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters