source: Dev/branches/jos-branch/server/classes/Auth.php @ 310

Last change on this file since 310 was 310, checked in by jkraaijeveld, 13 years ago

Merged rest-dojo-ui 305:309

File size: 1.2 KB
RevLine 
[256]1<?php
2
3require_once 'master.php';
4
5class Auth {
6
[285]7    public static function register($email, $password) {
8        $user_results = User::get(array("email" => $email));
[310]9        if (!empty($user_results)) {
[285]10            throw new Exception("User with email $email already exists.");
[256]11        }
[285]12        $salt = rand();
[310]13        $user = new User(null, array(
14                    'email' => $email,
15                    'passwordHash' => sha1($password . $salt),
16                    'passwordSalt' => $salt));
[285]17        $user->save();
18        return $user;
[256]19    }
[310]20
[285]21    public static function login($email, $password) {
22        $user_results = User::get(array("email" => $email));
23        if (empty($user_results)) {
24            throw new Exception("User with email $email not found.");
25        }
26        $user = $user_results[0];
[310]27        if (sha1($password . $user->passwordSalt) != $user->passwordHash) {
[285]28            throw new Exception("Wrong password.");
29        }
30        return $user;
31    }
[256]32
33    public static function restore($session) {
34        $user_results = User::get(array("uid" => $session));
[285]35        if (empty($user_results)) {
36            throw new Exception("Session with id $session not found.");
[256]37        }
[285]38        return $user_results[0];
[256]39    }
[310]40
[256]41}
42
43?>
Note: See TracBrowser for help on using the repository browser.