You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. use std::time::SystemTime;
  2. use std::io::Write;
  3. //use diesel::sql_types::Timestamp;
  4. use diesel::{Insertable, Queryable, Identifiable, Associations, AsExpression, FromSqlRow};
  5. //use bcrypt::{DEFAULT_COST, hash};
  6. use diesel::pg::Pg;
  7. use diesel::sql_types;
  8. use diesel::deserialize::{self, FromSql};
  9. use diesel::serialize::{self, ToSql, Output, IsNull};
  10. use diesel::backend::Backend;
  11. use libpso::character::settings;
  12. use elseware::schema::*;
  13. //const ELSEWHERE_COST: u32 = bcrypt::DEFAULT_COST;
  14. const ELSEWHERE_COST: u32 = 5;
  15. #[derive(Debug, AsExpression, FromSqlRow)]
  16. #[sql_type="sql_types::Binary"]
  17. pub struct EUserSettings(pub settings::UserSettings);
  18. impl std::ops::Deref for EUserSettings {
  19. type Target = settings::UserSettings;
  20. fn deref(&self) -> &Self::Target {
  21. &self.0
  22. }
  23. }
  24. #[derive(Queryable, Identifiable, Debug)]
  25. pub struct UserAccount {
  26. pub id: i32,
  27. pub username: String,
  28. pub password: String,
  29. pub guildcard: Option<i32>,
  30. pub team_id: Option<i32>,
  31. pub banned: bool,
  32. pub muted_until: SystemTime,
  33. pub created_at: SystemTime,
  34. }
  35. #[derive(Insertable)]
  36. #[table_name="user_accounts"]
  37. pub struct NewUser {
  38. username: String,
  39. password: String,
  40. }
  41. impl NewUser {
  42. pub fn new(username: String, password: String) -> NewUser {
  43. let crypt_password = bcrypt::hash(password, ELSEWHERE_COST).expect("could not hash password?");
  44. NewUser {
  45. username: username,
  46. password: crypt_password,
  47. }
  48. }
  49. }
  50. #[derive(Queryable, Identifiable, Associations)]
  51. #[belongs_to(UserAccount, foreign_key="user_id")]
  52. #[table_name="user_settings"]
  53. pub struct UserSettings {
  54. pub id: i32,
  55. pub user_id: i32,
  56. //settings: Vec<u8>,
  57. pub settings: EUserSettings,
  58. }
  59. #[derive(Insertable, Debug)]
  60. #[table_name="user_settings"]
  61. pub struct NewUserSettings {
  62. pub user_id: i32,
  63. pub settings: EUserSettings,
  64. }
  65. impl ToSql<sql_types::Binary, Pg> for EUserSettings {
  66. fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
  67. out.write_all(&self.0.as_bytes()[..])
  68. .map(|_| IsNull::No)
  69. .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
  70. }
  71. }
  72. impl FromSql<sql_types::Binary, Pg> for EUserSettings {
  73. fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
  74. let bytes_vec: Vec<u8> = <Vec<u8> as FromSql<sql_types::Binary, Pg>>::from_sql(bytes)?;
  75. let mut static_bytes = [0u8; 0x1160];
  76. static_bytes[..0x1160].clone_from_slice(&bytes_vec);
  77. Ok(EUserSettings(settings::UserSettings::from_bytes(static_bytes)))
  78. }
  79. }