-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f64ae7
commit f3987cd
Showing
4 changed files
with
62 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
admin-check | ||
# Admin Check | ||
|
||
![Version](https://img.shields.io/github/v/release/denizariyan/admin-check) | ||
|
||
Admin Check is an NPM package to check if the current script is running with admin privileges. | ||
|
||
Currently only available for Windows. It uses a native Windows command("net session") that is only available with admin privileges to check if the admin privileges are present. | ||
|
||
## Installation | ||
|
||
Use the package manager NPM to install Admin Check. | ||
|
||
```bash | ||
npm i admin-check | ||
``` | ||
|
||
## Usage | ||
|
||
Returns a `<Boolean>` which is true when admin privileges are present. | ||
```nodejs | ||
const admin = require("admin-check"); | ||
admin.check().then(result => { | ||
if (result) { | ||
// Do something when admin privileges are present | ||
} else { | ||
// Do something when admin privileges are NOT present | ||
} | ||
}); | ||
``` | ||
|
||
## Contributing | ||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
|
||
## License | ||
[MIT](https://github.com/denizariyan/admin-check/blob/main/LICENSE.md) |
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
var platform = require("os").platform(); | ||
const platform = require("os").platform(); | ||
|
||
function _check() { | ||
return new Promise(resolve => { | ||
return new Promise(resolve => { // Has to be a promise since Windows can take quite a while to respond under some circumstances | ||
if (platform == "win32" || platform == "win64") { | ||
require('child_process').exec('net session', function (err, stdout, stderr) { | ||
require('child_process').exec('net session', function (err, stdout, stderr) { // "net session" will return an error when admin privileges are not present | ||
if (err) { | ||
resolve(false); | ||
} else { | ||
resolve(true); | ||
} | ||
}); | ||
} else { | ||
throw new Error('Can not determine if admin priviliges are present or not') | ||
throw new Error('Can not determine if admin priviliges are present or not. This package is only compatible with Windows OS') | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
exports.check = async function () { | ||
var result = await _check(); | ||
return result | ||
|
||
} | ||
return await _check(); | ||
} |
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,9 @@ | ||
const admin = require("./index"); | ||
|
||
admin.check().then(result => { | ||
if (result) { | ||
console.log("1"); | ||
} else { | ||
console.log("0"); | ||
} | ||
}); |