Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
PUBLIC
/
surfer-okd
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 738b1d82
authored
Nov 17, 2016
by
Johannes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add local user admin tool
1 parent
dcb20866
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
0 deletions
admin
admin
0 → 100755
View file @
738b1d8
#!/usr/bin/env node
'use strict'
;
var
program
=
require
(
'commander'
),
safe
=
require
(
'safetydance'
),
bcrypt
=
require
(
'bcryptjs'
),
path
=
require
(
'path'
);
function
exit
(
error
)
{
if
(
error
)
console
.
error
(
error
);
process
.
exit
(
error
?
1
:
0
);
}
var
LOCAL_AUTH_FILE
=
path
.
resolve
(
process
.
env
.
LOCAL_AUTH_FILE
||
'./.users.json'
);
console
.
log
(
'Using local auth file: '
,
LOCAL_AUTH_FILE
);
function
addUser
(
options
)
{
if
(
!
options
.
username
)
exit
(
'missing --username'
);
if
(
!
options
.
password
)
exit
(
'missing --password'
);
var
users
=
safe
.
require
(
LOCAL_AUTH_FILE
);
if
(
!
users
)
users
=
{};
if
(
users
[
options
.
username
])
exit
(
'User already exists'
);
bcrypt
.
hash
(
options
.
password
,
8
,
function
(
error
,
hash
)
{
users
[
options
.
username
]
=
{
username
:
options
.
username
,
passwordHash
:
hash
};
safe
.
fs
.
writeFileSync
(
LOCAL_AUTH_FILE
,
JSON
.
stringify
(
users
,
null
,
4
));
console
.
log
(
'Done.'
);
});
}
function
editUser
(
options
)
{
if
(
!
options
.
username
)
exit
(
'missing --username'
);
if
(
!
options
.
password
)
exit
(
'missing --password'
);
var
users
=
safe
.
require
(
LOCAL_AUTH_FILE
);
if
(
!
users
)
users
=
{};
if
(
!
users
[
options
.
username
])
exit
(
'No such user'
);
bcrypt
.
hash
(
options
.
password
,
8
,
function
(
error
,
hash
)
{
users
[
options
.
username
]
=
{
username
:
options
.
username
,
passwordHash
:
hash
};
safe
.
fs
.
writeFileSync
(
LOCAL_AUTH_FILE
,
JSON
.
stringify
(
users
,
null
,
4
));
console
.
log
(
'Done.'
);
});
}
function
delUser
(
options
)
{
if
(
!
options
.
username
)
exit
(
'missing --username'
);
var
users
=
safe
.
require
(
LOCAL_AUTH_FILE
);
if
(
!
users
)
exit
(
'No such user'
);
if
(
!
users
[
options
.
username
])
exit
(
'No such user'
);
delete
users
[
options
.
username
];
safe
.
fs
.
writeFileSync
(
LOCAL_AUTH_FILE
,
JSON
.
stringify
(
users
,
null
,
4
));
console
.
log
(
'Done.'
);
}
function
listUsers
()
{
var
users
=
safe
.
require
(
LOCAL_AUTH_FILE
);
if
(
!
users
)
exit
(
'No users. Use `admin user-add` to add some.'
);
console
.
log
(
users
);
}
program
.
version
(
'0.1.0'
);
program
.
command
(
'user-add'
)
.
description
(
'Add local user'
)
.
option
(
'-u --username <username>'
,
'New username'
)
.
option
(
'-p --password <password>'
,
'New password'
)
.
action
(
addUser
);
program
.
command
(
'user-edit'
)
.
description
(
'Edit local user'
)
.
option
(
'-u --username <username>'
,
'Username'
)
.
option
(
'-p --password <password>'
,
'New password'
)
.
action
(
editUser
);
program
.
command
(
'user-del'
)
.
description
(
'Delete local user'
)
.
option
(
'-u --username <username>'
,
'Username'
)
.
action
(
delUser
);
program
.
command
(
'users'
)
.
description
(
'List local users'
)
.
action
(
listUsers
);
program
.
parse
(
process
.
argv
);
if
(
!
process
.
argv
.
slice
(
2
).
length
)
{
program
.
outputHelp
();
}
else
{
// https://github.com/tj/commander.js/issues/338
var
knownCommand
=
program
.
commands
.
some
(
function
(
command
)
{
return
command
.
_name
===
process
.
argv
[
2
];
});
if
(
!
knownCommand
)
{
console
.
error
(
'Unknown command: '
+
process
.
argv
[
2
]);
process
.
exit
(
1
);
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment