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
f70c3094
Commit
f70c3094
authored
Aug 28, 2018
by
Robert Czechowski
Browse files
Clean up, remove some warnings
parent
c9c38bc3
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/configreader_yaml.rs
View file @
f70c3094
extern
crate
linked_hash_map
;
use
self
::
linked_hash_map
::
LinkedHashMap
;
use
db_objects
::{
Contest
,
Taskgroup
,
Task
};
use
serde_yaml
;
...
...
src/db_conn_sqlite.rs
View file @
f70c3094
...
...
@@ -5,7 +5,7 @@ use self::rusqlite::Connection;
use
db_conn
::{
MedalConnection
,
MedalObject
};
use
db_objects
::
*
;
use
rand
::{
thread_rng
,
Rng
};
use
rand
::{
thread_rng
,
Rng
,
distributions
::
Alphanumeric
};
use
time
;
...
...
@@ -117,11 +117,11 @@ impl MedalConnection for Connection {
})
{
Ok
((
id
,
password_hash
,
salt
))
=>
{
//println!("{}, {}", password, password_hash.unwrap());
if
(
hash_password
(
&
password
,
&
salt
.unwrap
())
==
password_hash
.unwrap
()
)
{
if
hash_password
(
&
password
,
&
salt
.unwrap
())
==
password_hash
.unwrap
()
{
// Login okay, update session now!
let
session_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
session_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
now
=
time
::
get_time
();
self
.execute
(
"UPDATE session_user SET session_token = ?1, csrf_token = ?2, last_login = ?3, last_activity = ?3 WHERE id = ?4"
,
&
[
&
session_token
,
&
csrf_token
,
&
now
,
&
id
])
.unwrap
();
...
...
@@ -145,8 +145,8 @@ impl MedalConnection for Connection {
Ok
(
id
)
=>
{
// Login okay, update session now!
let
session_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
session_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
now
=
time
::
get_time
();
self
.execute
(
"UPDATE session_user SET session_token = ?1, csrf_token = ?2, last_login = ?3, last_activity = ?3 WHERE id = ?4"
,
&
[
&
session_token
,
&
csrf_token
,
&
now
,
&
id
])
.unwrap
();
...
...
@@ -158,8 +158,8 @@ impl MedalConnection for Connection {
}
fn
login_foreign
(
&
self
,
session
:
Option
<
String
>
,
foreign_id
:
u32
,
foreign_type
:
functions
::
UserType
,
firstname
:
String
,
lastname
:
String
)
->
Result
<
String
,()
>
{
let
session_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
session_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
now
=
time
::
get_time
();
println!
(
"x {} {}"
,
firstname
,
lastname
);
...
...
@@ -192,9 +192,9 @@ impl MedalConnection for Connection {
Ok
(
group_id
)
=>
{
// Login okay, create session_user!
let
session_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
gen_ascii_chars
(
)
.take
(
10
)
.collect
();
let
login_code
:
String
=
Some
(
'u'
)
.into_iter
()
.chain
(
thread_rng
()
.
gen_ascii_chars
(
))
let
session_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
csrf_token
:
String
=
thread_rng
()
.
sample_iter
(
&
Alphanumeric
)
.take
(
10
)
.collect
();
let
login_code
:
String
=
Some
(
'u'
)
.into_iter
()
.chain
(
thread_rng
()
.
sample_iter
(
&
Alphanumeric
))
.filter
(|
x
|
{
let
x
=
*
x
;
!
(
x
==
'l'
||
x
==
'I'
||
x
==
'1'
||
x
==
'O'
||
x
==
'o'
||
x
==
'0'
)})
.take
(
9
)
.collect
();
// todo: check for collisions
...
...
@@ -481,9 +481,9 @@ impl MedalConnection for Connection {
impl
MedalObject
<
Connection
>
for
Task
{
fn
save
(
&
mut
self
,
conn
:
&
Connection
)
{
conn
.query_row
(
"SELECT id FROM task WHERE taskgroup = ?1 AND location = ?2"
,
&
[
&
self
.taskgroup
,
&
self
.location
],
|
row
|
{
row
.get
(
0
)})
.and_then
(|
id
|
{
self
.set
I
d
(
id
);
Ok
(())
});
.and_then
(|
id
|
{
self
.set
_i
d
(
id
);
Ok
(())
})
.unwrap
();
// TODO handle error
;
let
id
=
match
self
.get
I
d
()
{
let
id
=
match
self
.get
_i
d
()
{
Some
(
id
)
=>
{
conn
.execute
(
"UPDATE task SET taskgroup = ?1, location = ?2, stars = ?3
...
...
@@ -499,7 +499,7 @@ impl MedalObject<Connection> for Task {
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
()
}
};
self
.set
I
d
(
id
);
self
.set
_i
d
(
id
);
}
}
...
...
@@ -507,9 +507,9 @@ impl MedalObject<Connection> for Task {
impl
MedalObject
<
Connection
>
for
Taskgroup
{
fn
save
(
&
mut
self
,
conn
:
&
Connection
)
{
conn
.query_row
(
"SELECT id FROM taskgroup WHERE contest = ?1 AND name = ?2"
,
&
[
&
self
.contest
,
&
self
.name
],
|
row
|
{
row
.get
(
0
)})
.and_then
(|
id
|
{
self
.set
I
d
(
id
);
Ok
(())
});
.and_then
(|
id
|
{
self
.set
_i
d
(
id
);
Ok
(())
})
.unwrap
();
// TODO handle error
;
let
id
=
match
self
.get
I
d
()
{
let
id
=
match
self
.get
_i
d
()
{
Some
(
id
)
=>
{
conn
.execute
(
"UPDATE taskgroup SET contest = ?1, name = ?2
...
...
@@ -525,7 +525,7 @@ impl MedalObject<Connection> for Taskgroup {
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
()
}
};
self
.set
I
d
(
id
);
self
.set
_i
d
(
id
);
for
mut
task
in
&
mut
self
.tasks
{
task
.taskgroup
=
id
;
task
.save
(
conn
);
...
...
@@ -536,9 +536,9 @@ impl MedalObject<Connection> for Taskgroup {
impl
MedalObject
<
Connection
>
for
Contest
{
fn
save
(
&
mut
self
,
conn
:
&
Connection
)
{
conn
.query_row
(
"SELECT id FROM contest WHERE location = ?1 AND filename = ?2"
,
&
[
&
self
.location
,
&
self
.filename
],
|
row
|
{
row
.get
(
0
)})
.and_then
(|
id
|
{
self
.set
I
d
(
id
);
Ok
(())
});
.and_then
(|
id
|
{
self
.set
_i
d
(
id
);
Ok
(())
})
.unwrap
();
// TODO handle error
;
let
id
=
match
self
.get
I
d
()
{
let
id
=
match
self
.get
_i
d
()
{
Some
(
id
)
=>
{
conn
.execute
(
"UPDATE contest SET location = ?1,filename = ?2,
...
...
@@ -558,7 +558,7 @@ impl MedalObject<Connection> for Contest {
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
()
}
};
self
.set
I
d
(
id
);
self
.set
_i
d
(
id
);
for
mut
taskgroup
in
&
mut
self
.taskgroups
{
taskgroup
.contest
=
id
;
taskgroup
.save
(
conn
);
...
...
@@ -581,12 +581,12 @@ impl MedalObject<Connection> for Participation {
impl
MedalObject
<
Connection
>
for
Submission
{
fn
save
(
&
mut
self
,
conn
:
&
Connection
)
{
match
self
.get
I
d
()
{
Some
(
id
)
=>
match
self
.get
_i
d
()
{
Some
(
_
id
)
=>
unimplemented!
(),
None
=>
{
conn
.execute
(
"INSERT INTO submission (task, session_user, grade, validated, nonvalidated_grade, subtask_identifier, value, date, needs_validation) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"
,
&
[
&
self
.task
,
&
self
.session_user
,
&
self
.grade
,
&
self
.validated
,
&
self
.nonvalidated_grade
,
&
self
.subtask_identifier
,
&
self
.value
,
&
self
.date
,
&
self
.needs_validation
])
.unwrap
();
self
.set
I
d
(
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
())
self
.set
_i
d
(
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
())
}
}
}
...
...
@@ -594,12 +594,12 @@ impl MedalObject<Connection> for Submission {
impl
MedalObject
<
Connection
>
for
Group
{
fn
save
(
&
mut
self
,
conn
:
&
Connection
)
{
match
self
.get
I
d
()
{
Some
(
id
)
=>
match
self
.get
_i
d
()
{
Some
(
_
id
)
=>
unimplemented!
(),
None
=>
{
conn
.execute
(
"INSERT INTO usergroup (name, groupcode, tag, admin) VALUES (?1, ?2, ?3, ?4)"
,
&
[
&
self
.name
,
&
self
.groupcode
,
&
self
.tag
,
&
self
.admin
])
.unwrap
();
self
.set
I
d
(
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
());
self
.set
_i
d
(
conn
.query_row
(
"SELECT last_insert_rowid()"
,
&
[],
|
row
|
{
row
.get
(
0
)})
.unwrap
());
}
}
}
...
...
src/db_objects.rs
View file @
f70c3094
...
...
@@ -96,12 +96,12 @@ pub struct Participation {
pub
start
:
Timespec
,
}
pub
trait
HasId
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
;
fn
set
I
d
(
&
mut
self
,
id
:
u32
);
}
impl
HasId
for
Submission
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
I
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Task
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
I
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Taskgroup
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
I
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Contest
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
I
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Group
{
fn
get
I
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
I
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
pub
trait
HasId
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
;
fn
set
_i
d
(
&
mut
self
,
id
:
u32
);
}
impl
HasId
for
Submission
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
_i
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Task
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
_i
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Taskgroup
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
_i
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Contest
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
_i
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
HasId
for
Group
{
fn
get
_i
d
(
&
self
)
->
Option
<
u32
>
{
self
.id
}
fn
set
_i
d
(
&
mut
self
,
id
:
u32
)
{
self
.id
=
Some
(
id
);}
}
impl
Contest
{
...
...
src/functions.rs
View file @
f70c3094
use
webfw_iron
::{
to_json
,
json_val
};
use
rusqlite
::
Connection
;
use
time
;
use
rand
::{
thread_rng
,
Rng
};
use
rand
::{
thread_rng
,
Rng
,
distributions
::
Alphanumeric
};
use
db_conn
::{
MedalConnection
,
MedalObject
};
use
db_conn
::{
MedalConnection
};
use
db_objects
::{
Submission
,
Group
};
...
...
@@ -102,7 +100,6 @@ pub fn show_contests<T: MedalConnection>(conn: &T) -> MedalValue {
pub
fn
show_contest
<
T
:
MedalConnection
>
(
conn
:
&
T
,
contest_id
:
u32
,
session_token
:
String
)
->
MedalValueResult
{
use
std
;
let
c
=
conn
.get_contest_by_id_complete
(
contest_id
);
let
mut
tasks
=
Vec
::
new
();
...
...
@@ -165,7 +162,7 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: u32, session_token
}
pub
fn
start_contest
<
T
:
MedalConnection
>
(
conn
:
&
T
,
contest_id
:
u32
,
session_token
:
String
,
csrf_token
:
String
)
->
MedalResult
<
()
>
{
let
mut
data
=
json_val
::
Map
::
new
();
let
data
=
json_val
::
Map
::
new
();
match
conn
.new_participation
(
session_token
,
contest_id
)
{
Ok
(
_
)
=>
Ok
(()),
...
...
@@ -351,7 +348,7 @@ pub fn add_group<T: MedalConnection>(conn: &T, session_token: String, csrf_token
return
Err
(
MedalError
::
AccessDenied
);
// CsrfError
}
let
group_code
:
String
=
Some
(
'g'
)
.into_iter
()
.chain
(
thread_rng
()
.
gen_ascii_chars
(
))
let
group_code
:
String
=
Some
(
'g'
)
.into_iter
()
.chain
(
thread_rng
()
.
sample_iter
(
&
Alphanumeric
))
.filter
(|
x
|
{
let
x
=
*
x
;
!
(
x
==
'l'
||
x
==
'I'
||
x
==
'1'
||
x
==
'O'
||
x
==
'o'
||
x
==
'0'
)})
.take
(
7
)
.collect
();
// todo: check for collisions
...
...
src/main.rs
View file @
f70c3094
...
...
@@ -66,9 +66,7 @@ fn read_config_from_file(filename: &str) -> Config {
oauth_user_data_url
:
None
,
}
}
}
}
fn
read_contest
(
p
:
&
path
::
PathBuf
)
->
Option
<
Contest
>
{
use
std
::
fs
::
File
;
...
...
src/webfw_iron.rs
View file @
f70c3094
//extern crate serde;
use
std
::
path
::
Path
;
use
iron_sessionstorage
::
traits
::
*
;
use
iron
::
prelude
::
*
;
use
iron
::{
status
,
AfterMiddleware
};
use
iron
::
modifiers
::
Redirect
;
use
iron
::
modifiers
::
RedirectRaw
;
...
...
@@ -19,15 +16,12 @@ use staticfile::Static;
use
iron_sessionstorage
::
SessionStorage
;
use
iron_sessionstorage
::
backends
::
SignedCookieBackend
;
use
rusqlite
::
Connection
;
use
urlencoded
::{
UrlEncodedBody
,
UrlEncodedQuery
};
use
urlencoded
::{
UrlEncodedBody
};
use
persistent
::
Write
;
use
handlebars_iron
::{
HandlebarsEngine
,
DirectorySource
,
Template
};
pub
use
handlebars_iron
::
handlebars
::
to_json
;
use
iron
::
prelude
::
*
;
use
iron_sessionstorage
::
traits
::
*
;
use
iron_sessionstorage
;
use
iron
;
use
reqwest
;
...
...
@@ -205,7 +199,7 @@ impl<'c, 'a: 'c, 'b: 'c + 'a, T> RequestAugmentMedalError<'c, 'a, 'b, T> for Res
}
fn
greet
(
req
:
&
mut
Request
)
->
IronResult
<
Response
>
{
fn
greet
(
_
req
:
&
mut
Request
)
->
IronResult
<
Response
>
{
// hier ggf. Daten aus dem Request holen
// Daten verarbeiten
...
...
@@ -279,6 +273,7 @@ fn contest_post(req: &mut Request) -> IronResult<Response> {
iexpect!
(
formdata
.get
(
"csrftoken"
))[
0
]
.to_owned
()
};
// TODO: Was mit dem Result?
let
startcontestresult
=
{
let
mutex
=
req
.get
::
<
Write
<
SharedDatabaseConnection
>>
()
.unwrap
();
...
...
@@ -569,7 +564,7 @@ fn user(req: &mut Request) -> IronResult<Response> {
let
(
template
,
data
)
=
{
// hier ggf. Daten aus dem Request holen
let
mutex
=
req
.get
::
<
Write
<
SharedDatabaseConnection
>>
()
.unwrap
();
let
conn
=
mutex
.lock
()
.unwrap_or_else
(|
e
|
e
.into_inner
());
let
_
conn
=
mutex
.lock
()
.unwrap_or_else
(|
e
|
e
.into_inner
());
// Antwort erstellen und zurücksenden
//functions::show_contests(&*conn)
...
...
@@ -593,7 +588,8 @@ struct OAuthAccess {
}
#[derive(Deserialize,
Debug)]
pub
struct
OAuthUserData
{
#[allow(non_snake_case)]
pub
struct
OAuthUserData
{
userID
:
Option
<
String
>
,
// documented as 'userId'
userId
:
Option
<
String
>
,
// sent as 'userID'
userType
:
String
,
...
...
@@ -608,7 +604,6 @@ pub struct OAuthUserData {
fn
oauth
(
req
:
&
mut
Request
)
->
IronResult
<
Response
>
{
use
reqwest
::
header
;
use
params
::{
Params
,
Value
};
use
std
::
io
::
Read
;
let
(
client_id
,
client_secret
,
access_token_url
,
user_data_url
)
=
{
let
mutex
=
req
.get
::
<
Write
<
SharedConfiguration
>>
()
.unwrap
();
...
...
@@ -621,7 +616,7 @@ fn oauth(req: &mut Request) -> IronResult<Response> {
}
};
let
(
state
,
scope
,
code
):
(
String
,
String
,
String
)
=
{
let
(
_
state
,
_
scope
,
code
):
(
String
,
String
,
String
)
=
{
let
map
=
req
.get_ref
::
<
Params
>
()
.unwrap
();
match
(
map
.find
(
&
[
"state"
]),
map
.find
(
&
[
"scope"
]),
map
.find
(
&
[
"code"
]))
{
...
...
@@ -715,7 +710,7 @@ impl Key for SharedConfiguration { type Value = ::Config; }
#[cfg(feature
=
"watch"
)]
pub
fn
get_handlebars_engine
()
->
impl
AfterMiddleware
{
//
/
HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
// HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
let
mut
hbse
=
HandlebarsEngine
::
new
();
hbse
.add
(
Box
::
new
(
DirectorySource
::
new
(
"./templates/"
,
".hbs"
)));
...
...
@@ -734,7 +729,7 @@ pub fn get_handlebars_engine() -> impl AfterMiddleware {
#[cfg(not(feature
=
"watch"
))]
pub
fn
get_handlebars_engine
()
->
impl
AfterMiddleware
{
//
/
HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
// HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
let
mut
hbse
=
HandlebarsEngine
::
new
();
hbse
.add
(
Box
::
new
(
DirectorySource
::
new
(
"./templates/"
,
".hbs"
)));
...
...
@@ -747,7 +742,7 @@ pub fn get_handlebars_engine() -> impl AfterMiddleware {
}
fn
cookie_warning
(
req
:
&
mut
Request
)
->
IronResult
<
Response
>
{
match
(
req
.get_session_token
()
)
{
match
req
.get_session_token
()
{
Some
(
session_token
)
=>
{
// TODO: Set session!
// TODO:
...
...
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