-
Notifications
You must be signed in to change notification settings - Fork 10
/
user.rs
54 lines (41 loc) · 1.3 KB
/
user.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use elasticsearch::Client;
use model::{Model, Error};
use auth::hasher;
pub struct User {
model: Model,
}
pub impl User {
fn id(&self) -> &self/~str { &self.model._id }
fn password(&self) -> ~str {
self.model.get_str(&~"password")
}
fn set_password(&mut self, hasher: hasher, password: ~str) -> bool {
let password = auth::password(hasher, password);
self.model.set_str(~"password", password)
}
fn verify_password(&self, hasher: hasher, password: ~str) -> bool {
hasher.verify(password, self.password())
}
fn create(&self) -> Result<(~str, uint), Error> {
self.model.create()
}
fn save(&self) -> Result<(~str, uint), Error> {
self.model.save()
}
fn delete(&self) {
use model::Model;
self.model.delete()
}
}
pub fn User(es: Client, hasher: hasher, index: ~str,
username: ~str, password: ~str) -> User {
let mut user = User { model: Model(es, index, ~"user", username) };
user.set_password(hasher, password);
user
}
pub fn find(es: Client, index: ~str, id: ~str) -> Option<User> {
model::find(es, index, ~"user", id).map(|model| User { model: *model })
}
pub fn all(es: Client, index: ~str) -> ~[User] {
model::all(es, index, ~"user").map(|model| User { model: *model })
}