added table creation
This commit is contained in:
@@ -3,7 +3,7 @@ use rand::{prelude::Rng, distributions::Alphanumeric };
|
|||||||
use oracle::{Connection, Error};
|
use oracle::{Connection, Error};
|
||||||
use log::{info, warn, error};
|
use log::{info, warn, error};
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse};
|
use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ pub struct User {
|
|||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Default)]
|
#[derive(Deserialize, Serialize, Debug, Default)]
|
||||||
pub struct NewUser {
|
pub struct NewUser {
|
||||||
netid: String,
|
net_id: String,
|
||||||
password: String,
|
password: String,
|
||||||
first_name: String,
|
first_name: String,
|
||||||
last_name: String
|
last_name: String
|
||||||
@@ -43,7 +43,7 @@ pub async fn login(request: HttpRequest, body: web::Json<Entry>) -> impl Respond
|
|||||||
println!("{:?}", body);
|
println!("{:?}", body);
|
||||||
match authenticate(net_id, password) {
|
match authenticate(net_id, password) {
|
||||||
Some(user) => {
|
Some(user) => {
|
||||||
Identity::login(&request.extensions(), net_id.into());
|
let id = Identity::login(&request.extensions(), net_id.into()).unwrap();
|
||||||
web::Json(user);
|
web::Json(user);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
},
|
},
|
||||||
@@ -62,10 +62,10 @@ pub async fn logout(user: Identity) -> impl Responder {
|
|||||||
pub async fn signup(request: HttpRequest, body: web::Json<NewUser>) -> impl Responder {
|
pub async fn signup(request: HttpRequest, body: web::Json<NewUser>) -> impl Responder {
|
||||||
let body = body.into_inner();
|
let body = body.into_inner();
|
||||||
|
|
||||||
match create_user(body.netid.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) {
|
match create_user(body.net_id.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
Identity::login(&request.extensions(), body.netid.clone());
|
Identity::login(&request.extensions(), body.net_id.clone()).unwrap();
|
||||||
web::Json(User { id: body.netid, first_name: body.first_name, last_name: body.last_name});
|
web::Json(User { id: body.net_id, first_name: body.first_name, last_name: body.last_name});
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -131,7 +131,7 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str
|
|||||||
|
|
||||||
info!("Creating user: {}", username);
|
info!("Creating user: {}", username);
|
||||||
let conn = Connection::connect(SQL_USERNAME, SQL_PASSWORD, "")?;
|
let conn = Connection::connect(SQL_USERNAME, SQL_PASSWORD, "")?;
|
||||||
let mut stmt = conn.statement("insert into student values(:net_id, :first_name, :last_name, :password, :salt)").build()?;
|
let mut stmt = conn.statement("insert into student values(:net_id, :first_name, :last_name, :pswd, :salt)").build()?;
|
||||||
|
|
||||||
let salt: String = rand::thread_rng().sample_iter(&Alphanumeric).take(SALT_LEN).map(char::from).collect();
|
let salt: String = rand::thread_rng().sample_iter(&Alphanumeric).take(SALT_LEN).map(char::from).collect();
|
||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
@@ -145,7 +145,7 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str
|
|||||||
hash_string += &format!("{:x}", value);
|
hash_string += &format!("{:x}", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
match stmt.execute_named(&[("net_id", &username), ("first_name", &first_name), ("last_name", &last_name), ("password", &hash_string), ("salt", &salt)]) {
|
match stmt.execute_named(&[("net_id", &username), ("first_name", &first_name), ("last_name", &last_name), ("pswd", &hash_string), ("salt", &salt)]) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!("User {} successfully created", username);
|
info!("User {} successfully created", username);
|
||||||
conn.commit()?;
|
conn.commit()?;
|
||||||
@@ -155,6 +155,19 @@ fn create_user(username: &str, password: &str, first_name: &str, last_name: &str
|
|||||||
conn.rollback()?;
|
conn.rollback()?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut new_table = conn.statement("create table week_:netid ( item_id number(5), foreign key (item_id) references menu_item (id))").build()?;
|
||||||
|
|
||||||
|
match new_table.execute_named(&[("net_id", &username)]) {
|
||||||
|
Ok(_) => {
|
||||||
|
info!("User {} week table created", username);
|
||||||
|
conn.commit()?;
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
warn!("Failed to create week table for {}", username);
|
||||||
|
conn.rollback()?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
conn.close()?;
|
conn.close()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use log::{info, warn, error};
|
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use actix_web::{web, get, post, web::Json, App, HttpResponse, HttpServer, Responder, middleware, cookie::Key};
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder, middleware, cookie::Key};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_identity::IdentityMiddleware;
|
use actix_identity::IdentityMiddleware;
|
||||||
@@ -8,8 +7,8 @@ use actix_session::{SessionMiddleware, storage::CookieSessionStore};
|
|||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
|
||||||
static PORT: u16 = 5000;
|
static PORT: u16 = 8000;
|
||||||
const ALLOWED_ORIGIN: &str = "http://localhost";
|
const ALLOWED_ORIGIN: &str = "http://localhost:8009";
|
||||||
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
|
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
|
||||||
@@ -36,6 +35,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.allowed_origin(ALLOWED_ORIGIN)
|
.allowed_origin(ALLOWED_ORIGIN)
|
||||||
.allowed_methods(vec!["GET","POST","DELETE"])
|
.allowed_methods(vec!["GET","POST","DELETE"])
|
||||||
.supports_credentials()
|
.supports_credentials()
|
||||||
|
.allow_any_header()
|
||||||
)
|
)
|
||||||
.wrap(IdentityMiddleware::default())
|
.wrap(IdentityMiddleware::default())
|
||||||
.wrap(SessionMiddleware::new(CookieSessionStore::default(), secret_key.clone()))
|
.wrap(SessionMiddleware::new(CookieSessionStore::default(), secret_key.clone()))
|
||||||
|
|||||||
Reference in New Issue
Block a user