Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bwinf
medal
Commits
0b3260ca
Commit
0b3260ca
authored
Feb 12, 2019
by
Robert Czechowski
Browse files
Add feature to reset admin pw with command line flag
parent
fa64c85d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
0b3260ca
debug
:
debug
:
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo run
--features
watch
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo run
--features
watch
--
-a
test
:
test
:
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo
test
--features
watch
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo
test
--features
watch
...
...
src/main.rs
View file @
0b3260ca
...
@@ -99,6 +99,10 @@ struct Opt {
...
@@ -99,6 +99,10 @@ struct Opt {
/// Port to listen on (default: from config file or 8080)
/// Port to listen on (default: from config file or 8080)
#[structopt(short
=
"p"
,
long
=
"port"
)]
#[structopt(short
=
"p"
,
long
=
"port"
)]
port
:
Option
<
u16
>
,
port
:
Option
<
u16
>
,
/// Reset password of admin user (user_id=1)
#[structopt(short
=
"a"
,
long
=
"reset-admin-pw"
)]
resetadminpw
:
bool
,
}
}
...
@@ -153,18 +157,36 @@ fn refresh_all_contests(conn : &mut Connection) {
...
@@ -153,18 +157,36 @@ fn refresh_all_contests(conn : &mut Connection) {
}
}
}
}
fn
add_admin_user
(
conn
:
&
mut
Connection
)
{
fn
add_admin_user
(
conn
:
&
mut
Connection
,
resetpw
:
bool
)
{
if
conn
.get_user_by_id
(
1
)
.is_none
()
{
let
mut
admin
=
match
conn
.get_user_by_id
(
1
)
{
None
=>
{
print!
(
"New Database. Creating new admin user with credentials 'admin':'test' … "
);
print!
(
"New Database. Creating new admin user with credentials 'admin':"
);
let
mut
admin
=
conn
.new_session
();
conn
.new_session
()
admin
.username
=
Some
(
"admin"
.into
());
match
admin
.set_password
(
"test"
)
{
},
None
=>
println!
(
"FAILED! (Password hashing error)"
),
Some
(
user
)
=>
{
_
=>
{
if
!
resetpw
{
conn
.save_session
(
admin
);
return
println!
(
"Done"
);
}
}
print!
(
"Request to reset admin password. Set credentials 'admin':"
);
user
}
_
=>
return
};
use
rand
::{
thread_rng
,
Rng
,
distributions
::
Alphanumeric
};
let
password
:
String
=
thread_rng
()
.sample_iter
(
&
Alphanumeric
)
.filter
(|
x
|
{
let
x
=
*
x
;
!
(
x
==
'l'
||
x
==
'I'
||
x
==
'1'
||
x
==
'O'
||
x
==
'o'
||
x
==
'0'
)})
.take
(
8
)
.collect
();
print!
(
"'{}' …"
,
&
password
);
admin
.username
=
Some
(
"admin"
.into
());
match
admin
.set_password
(
&
password
)
{
None
=>
println!
(
"FAILED! (Password hashing error)"
),
_
=>
{
conn
.save_session
(
admin
);
println!
(
"Done"
);
}
}
}
}
}
}
...
@@ -190,7 +212,9 @@ fn main() {
...
@@ -190,7 +212,9 @@ fn main() {
println!
(
"Hello, world!"
);
println!
(
"Hello, world!"
);
let
contest
=
conn
.get_contest_by_id_complete
(
1
);
let
contest
=
conn
.get_contest_by_id_complete
(
1
);
add_admin_user
(
&
mut
conn
);
add_admin_user
(
&
mut
conn
,
opt
.resetadminpw
);
println!
(
"Contest {}"
,
contest
.name
);
println!
(
"Contest {}"
,
contest
.name
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment