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

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

Merge from rest-dojo-ui 272-282

File size: 1.1 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));
9        if ( !empty($user_results) ) {
10            throw new Exception("User with email $email already exists.");
[256]11        }
[285]12        $salt = rand();
13        $user = new User(null,$email,sha1($password.$salt),$salt);
14        $user->save();
15        return $user;
[256]16    }
[285]17   
18    public static function login($email, $password) {
19        $user_results = User::get(array("email" => $email));
20        if (empty($user_results)) {
21            throw new Exception("User with email $email not found.");
22        }
23        $user = $user_results[0];
24        if (sha1($password.$user->passwordSalt) != $user->passwordHash) {
25            throw new Exception("Wrong password.");
26        }
27        return $user;
28    }
[256]29
30    public static function restore($session) {
31        $user_results = User::get(array("uid" => $session));
[285]32        if (empty($user_results)) {
33            throw new Exception("Session with id $session not found.");
[256]34        }
[285]35        return $user_results[0];
[256]36    }
37   
38}
39
40?>
Note: See TracBrowser for help on using the repository browser.