diff --git a/server/App/Controller/AuthController.php b/server/App/Controller/AuthController.php new file mode 100644 index 0000000..e06acfc --- /dev/null +++ b/server/App/Controller/AuthController.php @@ -0,0 +1,65 @@ + user_name_is_exists($uname)) { + return error_response("signup", "Username already exists"); + } + + $user = $auth -> signup($uname, $password); + + if(!$user) { + return error_response("signup", "Unknown error"); + } + + return success_response([ + "user_id" => $user -> get_id() + ]); + } + + public function signin() { + $uname = $_GET["username"]; + $password = $_GET["password"]; + $uname = sanitize_input($uname); + + $auth = new Auth(); + + $session = $auth -> signin($uname, $password); + + if(!$session) { + return error_response("signin", "Wrong username or password"); + } + + return success_response([ + "session" => [ + "token" => $session -> token, + "user" => [ + "id" => $session -> user() -> get_id(), + "name" => $session -> user() -> name + ] + ] + ]); + } + + public function signout() { + $token = sanitize_input($_GET["token"]); + $auth = new Auth(); + + if(!$auth -> signout($token)) { + return error_response("signout", "Unknown error"); + } + + return success_response(); + } +} \ No newline at end of file diff --git a/server/App/Controller/GroupsController.php b/server/App/Controller/GroupsController.php new file mode 100644 index 0000000..350a814 --- /dev/null +++ b/server/App/Controller/GroupsController.php @@ -0,0 +1,21 @@ + set_fields([ + "user_id", "title", "type", "position", "update_at", "create_at" + ]); + } + + public function change_position(int $new_pos) :bool { + + } +} \ No newline at end of file diff --git a/server/App/Entity/Session.php b/server/App/Entity/Session.php new file mode 100755 index 0000000..eb1d23a --- /dev/null +++ b/server/App/Entity/Session.php @@ -0,0 +1,61 @@ + set_fields([ + "user_id", "token", "state", "last_activity", "create_at" + ]); + } + + public function force_activity() { + $this -> last_activity = get_create_at_timestamp(); + $this -> update(); + } + + public function user() { + return $this -> user_instance; + } + + public function init_for_user(int $user_id) { + $this -> user_instance = new User(); + + if(!$this -> user() -> init_by_id($user_id)) { + return false; + } + + $this -> user_id = $this -> user() -> get_id(); + $this -> state = "active"; + $this -> token = gen_token($this -> user() -> get_id()); + $this -> last_activity = get_create_at_timestamp(); + $this -> create_at = get_create_at_timestamp(); + + $this -> create_new(); + + return $this; + } + + public function init_by_token(String $token) :bool { + return $this -> init_by_field("token", $token); + } + + public function kill() :bool { + if(!$this -> id) { + return throw new \Exception("Tried to kill uninitialized session"); + } + + $this -> state = "closed"; + return $this -> update(); + } +} \ No newline at end of file diff --git a/server/App/Entity/User.php b/server/App/Entity/User.php new file mode 100755 index 0000000..629ecc3 --- /dev/null +++ b/server/App/Entity/User.php @@ -0,0 +1,17 @@ + set_fields([ + "name", "passhash", "create_at" + ]); + } +} \ No newline at end of file diff --git a/server/App/Models/Auth.php b/server/App/Models/Auth.php new file mode 100755 index 0000000..e879b88 --- /dev/null +++ b/server/App/Models/Auth.php @@ -0,0 +1,59 @@ + db() -> query($sql); + $d = $result -> fetch_assoc(); + return $d["COUNT(*)"] ? true : false; + } + + public function signup(String $uname, String $password) { + $passhash = sha1($password); + + $user = new User(); + $user -> name = $uname; + $user -> passhash = $passhash; + $user -> create_at = get_create_at_timestamp(); + + return $user -> create_new() ? $user : false; + } + + public function signin(String $uname, String $password) { + $passhash = sha1($password); + + $tablename = User::get_tablename(); + $sql = "SELECT `id` FROM `{$tablename}` WHERE `name`='{$uname}' AND `passhash`='{$passhash}'"; + $result = app() -> db() -> query($sql); + + if(!$result) { + return false; + } + + $d = $result -> fetch_assoc(); + if(!isset($d["id"])) { + return false; + } + + $session = new Session(); + + return $session -> init_for_user($d["id"]) ?? false; + } + + public function signout(String $token) :bool { + $session = new Session(); + if(!$session -> init_by_token($token)) { + return false; + } + + return $session -> kill(); + } +} \ No newline at end of file diff --git a/server/App/Models/Groups.php b/server/App/Models/Groups.php new file mode 100644 index 0000000..085321b --- /dev/null +++ b/server/App/Models/Groups.php @@ -0,0 +1,15 @@ + linking("GET", "signup", function(){ - $uname = $_GET["username"]; - $password = $_GET["password"]; - - $auth = new Auth(); - - if($auth -> user_name_is_exists($uname)) { - return error_response("signup", "Username already exists"); - } - - $user = $auth -> signup($uname, $password); - - if(!$user) { - return error_response("signup", "Unknown error"); - } - - return success_response([ - "user_id" => $user -> get_id() - ]); + $auth_controller = new AuthController(); + $auth_controller -> signup(); }); $router -> linking("GET", "signin", function(){ - $uname = $_GET["username"]; - $password = $_GET["password"]; - - $auth = new Auth(); - - $session = $auth -> signin($uname, $password); - - if(!$session) { - return error_response("signin", "Wrong username or password"); - } - - return success_response([ - "session" => [ - "token" => $session -> token, - "user" => [ - "id" => $session -> user() -> get_id(), - "name" => $session -> user() -> name - ] - ] - ]); + $auth_controller = new AuthController(); + $auth_controller -> signin(); }); $router -> linking("GET", "signout", function(){ - $token = sanitize_input($_GET["token"]); - $auth = new Auth(); - - if(!$auth -> signout($token)) { - return error_response("signout", "Unknown error"); - } - - return success_response(); + $auth_controller = new AuthController(); + $auth_controller -> signout(); }); } diff --git a/server/Kernel/Classes/Auth.php b/server/Kernel/Classes/Auth.php deleted file mode 100755 index 1f1c581..0000000 --- a/server/Kernel/Classes/Auth.php +++ /dev/null @@ -1,61 +0,0 @@ - db() -> query($sql); - $d = $result -> fetch_assoc(); - return $d["COUNT(*)"] ? true : false; - } - - public function signup(String $uname, String $password) { - $passhash = sha1($password); - $uname = sanitize_input($uname); - - $user = new User(); - $user -> name = $uname; - $user -> passhash = $passhash; - $user -> create_at = get_create_at_timestamp(); - - return $user -> create_new() ? $user : false; - } - - public function signin(String $uname, String $password) { - $passhash = sha1($password); - $uname = sanitize_input($uname); - - $tablename = User::get_tablename(); - $sql = "SELECT `id` FROM `{$tablename}` WHERE `name`='{$uname}' AND `passhash`='{$passhash}'"; - $result = app() -> db() -> query($sql); - - if(!$result) { - return false; - } - - $d = $result -> fetch_assoc(); - if(!isset($d["id"])) { - return false; - } - - $session = new Session(); - - return $session -> init_for_user($d["id"]) ?? false; - } - - public function signout(String $token) :bool { - $session = new Session(); - if(!$session -> init_by_token($token)) { - return false; - } - - return $session -> kill(); - } -} \ No newline at end of file diff --git a/server/Kernel/Entity/Session.php b/server/Kernel/Entity/Session.php deleted file mode 100755 index 4f76406..0000000 --- a/server/Kernel/Entity/Session.php +++ /dev/null @@ -1,61 +0,0 @@ - set_fields([ - "user_id", "token", "state", "last_activity", "create_at" - ]); - } - - public function force_activity() { - $this -> last_activity = get_create_at_timestamp(); - $this -> update(); - } - - public function user() { - return $this -> user_instance; - } - - public function init_for_user(int $user_id) { - $this -> user_instance = new User(); - - if(!$this -> user() -> init_by_id($user_id)) { - return false; - } - - $this -> user_id = $this -> user() -> get_id(); - $this -> state = "active"; - $this -> token = gen_token($this -> user() -> get_id()); - $this -> last_activity = get_create_at_timestamp(); - $this -> create_at = get_create_at_timestamp(); - - $this -> create_new(); - - return $this; - } - - public function init_by_token(String $token) :bool { - return $this -> init_by_field("token", $token); - } - - public function kill() :bool { - if(!$this -> id) { - return throw new \Exception("Tried to kill uninitialized session"); - } - - $this -> state = "closed"; - return $this -> update(); - } -} \ No newline at end of file diff --git a/server/Kernel/Entity/User.php b/server/Kernel/Entity/User.php deleted file mode 100755 index 84f3f07..0000000 --- a/server/Kernel/Entity/User.php +++ /dev/null @@ -1,21 +0,0 @@ - set_fields([ - "name", "passhash", "create_at" - ]); - } - - public function init_by_id(int $id) :bool { - return $this -> init_by_field("id", $id); - } -} \ No newline at end of file diff --git a/server/libs/DataStorage.php b/server/libs/DataStorage.php index 8b0eb29..55f25da 100755 --- a/server/libs/DataStorage.php +++ b/server/libs/DataStorage.php @@ -50,6 +50,8 @@ } public function update() :bool { + !$this -> id and throw new \Exception("Tried updating of uninizialized object"); + if(!count($this -> changed_fields)) { return true; } @@ -66,4 +68,16 @@ $sql = "UPDATE `{$tablename}` SET {$fields} WHERE `id`='{$this -> id}' LIMIT 1"; return app() -> db() -> query($sql); } + + public function init_by_id(int $id) :bool { + return $this -> init_by_field("id", $id); + } + + public function remove() :bool { + !$this -> id and throw new \Exception("Tried removing of uninizialized object"); + + $tablename = self::get_tablename(); + $sql = "DELETE FROM `{$tablename}` WHERE `id`='{$this -> id}'"; + return app() -> db() -> query($sql); + } } \ No newline at end of file