source: Dev/branches/rest-dojo-ui/server/classes/Auth.php @ 303

Last change on this file since 303 was 303, checked in by hendrikvanantwerpen, 13 years ago

[Server] Refactored model classes with some meta-programming. Specific classes only define their fields and inherit from class RdfObject?. Changes to handle the new model objects correctly.
[Client] Added rft/store module for uniform resource access. Removed dependencies on 'uid' field name. Added support for references without loading full object nor exposing uri.
[Client] Added reset() to QuestionWidget?.
[RDFAPI] Fixed PHP warning.

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