Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit eaa6633

Browse files
authored
add username for user (#30)
add username for user
2 parents 3900b82 + 4a22e52 commit eaa6633

19 files changed

Lines changed: 158 additions & 80 deletions

File tree

Cargo.lock

Lines changed: 31 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ num_cpus = "1.10.0"
3030
ammonia = "2.0.0"
3131
maplit = "1.0.1"
3232
md5 = "0.6.1"
33+
regex = "1.3.1"
34+
lazy_static = "1.4.0"

docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ until PGPASSWORD=${POSTGRES_PASSWORD} psql -h ${DATABASE_HOST} -U ${POSTGRES_USE
1010
done
1111

1212
>&2 echo "Postgres is up - executing command"
13-
cd /app && ls . && diesel migration run && exec $@
13+
cd /app && diesel migration run && exec $@
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "users" DROP COLUMN "username";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE "users" ADD COLUMN "username" VARCHAR;
2+
3+
UPDATE users
4+
SET username = CONCAT('id', users.id)
5+
FROM users comp
6+
WHERE users.id = comp.id;
7+
8+
ALTER TABLE "users" ALTER COLUMN "username" SET NOT NULL;

src/handlers/account/create.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Create account
22
use actix_base::prelude::*;
33
use actix_web::*;
4+
use uuid::Uuid;
45

56
use crate::app_state::DbExecutor;
67
use crate::consts;
@@ -38,6 +39,7 @@ impl Handler<AccountCreate> for DbExecutor {
3839
let new_account = UserNew {
3940
email: msg.email,
4041
password: hasher::hash_password(&msg.password, consts::SALT),
42+
username: format!("{}", Uuid::new_v4()),
4143
};
4244

4345
User::create(&self.conn, new_account).ok_or(AccountCreateError::EmailExists)

src/handlers/account/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Handler<SessionCreate> for DbExecutor {
4545
type Result = Result<SessionToken, SessionCreateError>;
4646

4747
fn handle(&mut self, msg: SessionCreate, _: &mut Self::Context) -> Self::Result {
48-
let credentials = UserNew {
48+
let credentials = Credentials {
4949
email: msg.email,
5050
password: hasher::hash_password(&msg.password, consts::SALT),
5151
};

src/handlers/account/update.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub enum AccountUpdateError {
1313

1414
#[fail(display = "failed_to_update")]
1515
Failed,
16+
17+
#[fail(display = "username_empty")]
18+
UsernameEmpty,
19+
20+
#[fail(display = "username_incorrect")]
21+
UsernameIncorrect,
1622
}
1723

1824
impl_response_error_for!(AccountUpdateError as BadRequest);
@@ -23,6 +29,7 @@ pub struct AccountUpdate {
2329
pub requester_id: i32,
2430
pub display_name: String,
2531
pub gravatar_email: String,
32+
pub username: String,
2633
}
2734

2835
impl Message for AccountUpdate {
@@ -33,12 +40,19 @@ impl Handler<AccountUpdate> for DbExecutor {
3340
type Result = Result<User, AccountUpdateError>;
3441

3542
fn handle(&mut self, msg: AccountUpdate, _: &mut Self::Context) -> Self::Result {
36-
User::update(
37-
&self.conn,
38-
msg.requester_id,
39-
msg.display_name,
40-
msg.gravatar_email,
41-
)
42-
.ok_or(AccountUpdateError::Failed)
43+
if msg.username.trim().is_empty() {
44+
Err(AccountUpdateError::UsernameEmpty)
45+
} else if !crate::validators::check_username(msg.username.as_str()) {
46+
Err(AccountUpdateError::UsernameIncorrect)
47+
} else {
48+
User::update(
49+
&self.conn,
50+
msg.requester_id,
51+
msg.display_name,
52+
msg.gravatar_email,
53+
msg.username,
54+
)
55+
.ok_or(AccountUpdateError::Failed)
56+
}
4357
}
4458
}

src/handlers/users/cards_by_author.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::app_state::DbExecutor;
44
use crate::models::*;
55

66
pub struct GetCardsByAuthor {
7-
pub author_id: i32,
7+
pub author_username: String,
88
}
99

1010
impl Message for GetCardsByAuthor {
@@ -15,6 +15,10 @@ impl Handler<GetCardsByAuthor> for DbExecutor {
1515
type Result = Option<Vec<Card>>;
1616

1717
fn handle(&mut self, msg: GetCardsByAuthor, _ctx: &mut Self::Context) -> Self::Result {
18-
Some(Card::find_all_by_author(&self.conn, msg.author_id))
18+
if let Some(user) = User::find_by_username(&self.conn, msg.author_username) {
19+
Some(Card::find_all_by_author(&self.conn, user.id))
20+
} else {
21+
None
22+
}
1923
}
2024
}

src/handlers/users/get_user.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::app_state::DbExecutor;
44
use crate::models::*;
55

66
pub struct GetUser {
7-
pub user_id: i32,
7+
pub username: String,
88
}
99

1010
impl Message for GetUser {
@@ -15,6 +15,6 @@ impl Handler<GetUser> for DbExecutor {
1515
type Result = Option<User>;
1616

1717
fn handle(&mut self, msg: GetUser, _ctx: &mut Self::Context) -> Self::Result {
18-
User::find_by_id(&self.conn, msg.user_id)
18+
User::find_by_username(&self.conn, msg.username)
1919
}
2020
}

0 commit comments

Comments
 (0)