Changeset 285 for Dev


Ignore:
Timestamp:
02/24/12 10:03:02 (13 years ago)
Author:
jkraaijeveld
Message:

Merge from rest-dojo-ui 272-282

Location:
Dev/branches/jos-branch/server
Files:
2 deleted
16 edited
1 copied

Legend:

Unmodified
Added
Removed
  • Dev/branches/jos-branch/server/api.php

    r263 r285  
    4646
    4747        $user = null;
    48         $data = null;
    4948        if (!empty($request->data)) {
    50             $data = json_decode($request->data, true);
    51         }
    52         if (!empty($data)) {
    53             $user = Auth::login($data['username'], sha1($data['password']));
     49            $user = Auth::login($request->data['email'], $request->data['password']);
    5450            if (!$user) {
    55                 throw new ResponseException("Incorrect username and password", Response::UNAUTHORIZED);
     51                throw new ResponseException("Incorrect email and password", Response::UNAUTHORIZED);
    5652            }
    5753            set_session_cookie($response, $user);
     
    5955            $user = restore_session($response);
    6056        }
    61         $response->body = json_encode($user);
     57        $response->body = $user;
     58        return $response;
     59    }
     60
     61}
     62
     63/**
     64 *  @uri /register
     65 */
     66class RegisterResource extends Resource {
     67
     68    function post($request) {
     69        $response = new Response($request);
     70
     71        $user = null;
     72        if (!empty($request->data)) {
     73            $user = Auth::register($request->data['email'], $request->data['password']);
     74            set_session_cookie($response, $user);
     75        } else {
     76            throw new ResponseException("No email and password provided.",Response::BADREQUEST);
     77        }
     78        $response->body = $user;
    6279        return $response;
    6380    }
     
    83100        $type = $this->getType($request);
    84101        $objects = $type::get(array());
    85         //$objects = array_map(function($val) { return array('uid' => $val->uid); }, $objects);
    86 
    87         $response->body = json_encode($objects);
     102
     103        $response->body = $objects;
    88104        return $response;
    89105    }
     
    94110
    95111        $type = $this->getType($request);
    96         $data = json_decode($request->data);
    97112        $onlyAdd = $request->ifNoneMatch('*');
    98113        $onlyUpdate = $request->ifMatch('*');
    99114
    100115        $object = FALSE;
    101         if (isset($data->uid)) {
    102             $objects = $type::get(array('uid' => $data->uid));
     116        if (isset($request->data->uid)) {
     117            $objects = $type::get(array('uid' => $request->data->uid));
    103118            if (!empty($objects)) {
    104119                $object = $objects[0];
     
    116131            $response->code = Response::OK;
    117132        }
    118         foreach ($data as $key => $val) {
     133        foreach ($request->data as $key => $val) {
    119134            $object->$key = $val;
    120135        }
    121         $object->save();
    122 
    123         $response->body = json_encode($object);
    124         return $response;
    125     }
    126 
     136        if (!$object->save()) {
     137            throw new ResponseException("Save failed", Response::INTERNALSERVERERROR);
     138        }
     139
     140        $response->body = $object;
     141        return $response;
     142    }
     143
     144    function put($request) {
     145        return $this->post($request);
     146    }
     147   
    127148}
    128149
     
    150171        }
    151172
    152         $response->body = json_encode($objects[0]);
    153         return $response;
    154     }
    155 
    156     function put($request) {
     173        $response->body = $objects[0];
     174        return $response;
     175    }
     176
     177    function post($request) {
    157178        $response = new Response($request);
    158179        restore_session($response);
    159180
    160181        $info = $this->getTypeAndUid($request);
    161         $data = json_decode($request->data);
    162182        $onlyAdd = $request->ifNoneMatch('*');
    163183        $onlyUpdate = $request->ifMatch('*');
     
    179199            $response->code = Response::OK;
    180200        }
    181         foreach ($data as $key => $val) {
     201        foreach ($request->data as $key => $val) {
    182202            $object->$key = $val;
    183203        }
    184         $object->save();
    185 
    186         $response->body = json_encode($object);
    187         return $response;
     204        if (!$object->save()) {
     205            throw new ResponseException("Save failed", Response::INTERNALSERVERERROR);
     206        }
     207
     208        $response->body = $object;
     209        return $response;
     210    }
     211
     212    function put($request) {
     213        return $this->post($request);
    188214    }
    189215
    190216    function delete($request) {
    191         restore_session();
     217        restore_session(new Response($request));
    192218        throw new ReponseException("Delete not implemented", Response::METHODNOTALLOWED);
    193219    }
     
    202228    $request->baseUri = $baseUri;
    203229}
    204 
     230$request->data = Marshaller::unmarshall(json_decode($request->data));
    205231try {
    206232    $resource = $request->loadResource();
     
    208234} catch (ResponseException $e) {
    209235    $response = $e->response($request);
    210     $response->body = json_encode(array('errorMsg' => $response->body));
     236    $response->body = array('errorMsg' => $response->body);
    211237} catch (Exception $e) {
    212238    $response = new Response($request);
    213239    $response->code = Response::INTERNALSERVERERROR;
    214     $response->body = json_encode(array('errorMsg' => "Unhandled exception: " . $e));
     240    $response->body = array('errorMsg' => "Unhandled exception: " . $e);
    215241}
    216242add_default_headers($response);
     243$response->body = json_encode(Marshaller::marshall($response->body));
    217244$response->output();
     245
    218246?>
  • Dev/branches/jos-branch/server/classes/Auth.php

    r256 r285  
    55class Auth {
    66
    7     public static function login($username, $password) {
    8         $user_results = User::get(array("name" => $username));
    9         if (!empty($user_results)) {
    10             $user = $user_results[0];
    11             if (sha1($password) == $user->password) {
    12                 return $user;
    13             }
     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.");
    1411        }
    15         return false;
     12        $salt = rand();
     13        $user = new User(null,$email,sha1($password.$salt),$salt);
     14        $user->save();
     15        return $user;
     16    }
     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;
    1628    }
    1729
    1830    public static function restore($session) {
    1931        $user_results = User::get(array("uid" => $session));
    20         if (!empty($user_results)) {
    21             return $user_results[0];
     32        if (empty($user_results)) {
     33            throw new Exception("Session with id $session not found.");
    2234        }
    23         return false;
     35        return $user_results[0];
    2436    }
    2537   
  • Dev/branches/jos-branch/server/classes/models/Answer.php

    r269 r285  
    1818        public static function create($obj)
    1919        {
    20                 return new Answer($obj->uid, $obj->question, $obj->values)
     20                return new Answer($obj->uid, $obj->question, $obj->values);
    2121        }
    2222
  • Dev/branches/jos-branch/server/classes/models/AnswerSet.php

    r268 r285  
    187187                return $answers;
    188188        }
     189
     190    public static function create($obj) {
     191        return new AnswerSet($obj->uid, $obj->survey, $obj->respondent, $obj->datetime, $obj->answers);
     192    }
     193   
    189194}
    190195
  • Dev/branches/jos-branch/server/classes/models/Application.php

    r256 r285  
    113113        return $applications;
    114114        }
     115
     116    public static function create($obj) {
     117        return new Application($obj->uid, $obj->title, $obj->description, $obj->style);
     118    }
    115119}
    116120
  • Dev/branches/jos-branch/server/classes/models/ApplicationInstance.php

    r263 r285  
    167167
    168168        }
     169
     170    public static function create($obj) {
     171        return new ApplicationInstance($obj->uid, $obj->application,
     172                $obj->starttime, $obj->endtime, $obj->open, $obj->playerresults,
     173                $obj->groupresults, $obj->periodicresults);
     174    }
    169175}
    170176?>
  • Dev/branches/jos-branch/server/classes/models/ResearchToolObject.php

    r256 r285  
    1111
    1212
    13 abstract class ResearchToolObject {
     13abstract class ResearchToolObject implements ResearchToolObjectInterface {
    1414
    15         public $uid;
     15        public $uid = null;
    1616
     17    public function getUid() {
     18        return $this->uid;
     19    }
     20   
    1721        public function evaluate()
    1822        {
     
    7983}
    8084
     85interface ResearchToolObjectInterface {
     86    function getUid();
     87    static function get($arguments);
     88    static function create($obj);
     89    function evaluate();
     90    function save();
     91}
     92
    8193?>
  • Dev/branches/jos-branch/server/classes/models/Respondent.php

    r256 r285  
    1616
    1717        public $email;
    18         public $password;
     18        public $passwordHash;
     19        public $passwordSalt;
    1920
    2021        /**
     
    2425         * @param type password
    2526         */
    26         public function __construct($uid = null, $email = null, $password = null)
     27        public function __construct($uid = null, $email = null, $passwordHash = null, $passwordSalt = null)
    2728        {
    2829        if(!isset($uid))
     
    3334        $this->uid = $uid;
    3435        $this->email = $email;
    35         $this->password = $password;
     36        $this->passwordHash = $passwordHash;
     37        $this->passwordSalt = $passwordSalt;
    3638        }
    3739    /**
     
    6466        $model->add(new Statement($resourceRespondent,$predicateName,$literalRespondentName));                 
    6567
    66         $literalPassword = new Literal($this->password);
    67         $predicatePassword = new Resource(PASSWORD);
    68         $model->add(new Statement($resourceRespondent,$predicatePassword,$literalPassword));
     68        $literalPasswordHash = new Literal($this->passwordHash);
     69        $predicatePasswordHash = new Resource(PASSWORDHASH);
     70        $model->add(new Statement($resourceRespondent,$predicatePasswordHash,$literalPasswordHash));
     71       
     72        $literalPasswordSalt = new Literal($this->passwordSalt);
     73        $predicatePasswordSalt = new Resource(PASSWORDSALT);
     74        $model->add(new Statement($resourceRespondent,$predicatePasswordSalt,$literalPasswordSalt));
     75       
    6976                $model->saveAs(Respondent::$filename, 'rdf');
    7077                return true;
     
    8592            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    8693            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    87             SELECT ?uid, ?email, ?password
     94            SELECT ?uid, ?email, ?passwordHash, ?passwordSalt
    8895            WHERE       
    8996            {
     
    9198                                        predicates:uid ?uid ;
    9299                                        predicates:email ?email ;
    93                                         predicates:password ?password ;
     100                                        predicates:passwordHash ?passwordHash ;
     101                                        predicates:passwordSalt ?passwordSalt ;
    94102                                        ' . ResearchToolObject::createArguments($arguments) .  '
    95103            }';
     
    102110            foreach($results as $result)
    103111            {
    104                     $respondents[] = new Respondent($result['?uid']->label, $result['?email']->label, $result['?password']->label);
     112                    $respondents[] = new Respondent($result['?uid']->label, $result['?email']->label, $result['?passwordHash']->label, $result['?passwordSalt']->label);
    105113            }
    106114        }
    107115        return $respondents;
    108116        }
     117
     118    public static function create($obj) {
     119        return new Respondent($obj->uid, $obj->email, $obj->passwordHash, $obj->passwordSalt);
     120    }
     121
    109122}
    110123
    111        
    112 
    113 
    114 
     124?>
  • Dev/branches/jos-branch/server/classes/models/ResultSet.php

    r263 r285  
    158158                return $answersets;
    159159        }
     160
     161    public static function create($obj) {
     162        return new ResultSet($obj->uid, $obj->answersets, $obj->playerresults,
     163                $obj->groupresults, $obj->periodicresults);
     164    }
    160165}
  • Dev/branches/jos-branch/server/classes/models/Session.php

    r263 r285  
    2424         * @param type $title
    2525         * @param type $creator
    26          * @param type $datetime
     26         * @param type $creationdate
    2727         * @param type $pipeline
    2828         */
    29         public function __construct($uid = null, $title = null, $creator = null, $datetime = null, $pipeline = null)
     29        public function __construct($uid = null, $title = null, $creator = null, $creationdate = null, $pipeline = null)
    3030        {
    3131                if(!isset($uid))
     
    3636                $this->title = $title;
    3737                $this->creator = $creator;
    38                 $this->creationdate = $datetime;
     38                $this->creationdate = $creationdate;
    3939                $this->pipeline = $pipeline;
    4040        }
     
    141141                        }
    142142                }
    143 
    144                 $model->saveAs(Session::$filename, 'rdf');
    145                 return true;
     143               
     144                return $model->saveAs(Session::$filename, 'rdf');
    146145        }
    147146
     
    320319        }
    321320
     321    public static function create($obj) {
     322        return new Session($obj->uid, $obj->title, $obj->creator,
     323                $obj->creationdate, $obj->pipeline);
     324    }
     325
    322326}       
    323327
    324 
    325328?>
  • Dev/branches/jos-branch/server/classes/models/SessionInstance.php

    r263 r285  
    235235        }
    236236
     237    public static function create($obj) {
     238        return new SessionInstance($obj->uid, $obj->title, $obj->location,
     239                $obj->facilitator, $obj->starttime, $obj->endtime, $obj->notes,
     240                $obj->session, $obj->resultset);
     241    }
    237242
    238243}
     244
     245?>
  • Dev/branches/jos-branch/server/classes/models/Survey.php

    r263 r285  
    169169    }
    170170
     171    public static function create($obj) {
     172        return new Survey($obj->uid, $obj->title, $obj->description,
     173                $obj->creator, $obj->questions);
     174    }
    171175
    172176}
  • Dev/branches/jos-branch/server/classes/models/SurveyInstance.php

    r269 r285  
    2626     * @param type $answersets: A list of answersets.
    2727     */
    28     public function __construct($uid, $survey, $starttime, $endtime, $open, $presetanswers, $answersets)
     28        public function __construct($uid = null, $survey = null, $starttime = null, $endtime = null, $open = null, $presetanswers = null, $answersets = null)
    2929    {
    3030        if(!isset($uid))
  • Dev/branches/jos-branch/server/classes/models/User.php

    r256 r285  
    1717   
    1818    public $email;
    19     public $password;
     19    public $passwordHash;
     20    public $passwordSalt;
    2021   
    2122    /**
     
    2324     * If the user does not yet exist in the database, call with null as first parameter
    2425     */
    25     public function __construct($uid = null, $email = null, $password = null) {
     26    public function __construct($uid = null, $email = null, $passwordHash = null, $passwordSalt = null) {
    2627        if(!isset($uid))
    2728        {
     
    3031        $this->uid = $uid;
    3132                $this->email = $email;
    32         $this->password = sha1($password);
     33        $this->passwordHash = $passwordHash;
     34        $this->passwordSalt = $passwordSalt;
    3335        }
    3436
     
    6062        $model->add(new Statement($resourceUser,$predicateName,$literalUserName));                     
    6163
    62         $literalPassword = new Literal($this->password);
    63         $predicatePassword = new Resource(PASSWORD);
    64                 $model->add(new Statement($resourceUser,$predicatePassword,$literalPassword));
     64        $literalPasswordHash = new Literal($this->passwordHash);
     65        $predicatePasswordHash = new Resource(PASSWORDHASH);
     66                $model->add(new Statement($resourceUser,$predicatePasswordHash,$literalPasswordHash));
     67
     68        $literalPasswordSalt = new Literal($this->passwordSalt);
     69        $predicatePasswordSalt = new Resource(PASSWORDSALT);
     70                $model->add(new Statement($resourceUser,$predicatePasswordSalt,$literalPasswordSalt));
    6571
    6672                $model->saveAs(User::$filename, 'rdf');
     
    8288            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    8389            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    84             SELECT ?uid, ?email, ?password
     90            SELECT ?uid, ?email, ?passwordHash, ?passwordSalt
    8591            WHERE       
    8692            {
     
    8894                                        predicates:uid ?uid ;
    8995                                        predicates:email ?email ;
    90                                         predicates:password ?password ;
     96                                        predicates:passwordHash ?passwordHash ;
     97                                        predicates:passwordSalt ?passwordSalt ;
    9198                                        ' . ResearchToolObject::createArguments($arguments) . '
    9299            }';
     
    99106            foreach($results as $result)
    100107            {
    101                                 $users[] = new User($result['?uid']->label, $result['?email']->label, $result['?password']->label);
     108                                $users[] = new User($result['?uid']->label, $result['?email']->label, $result['?passwordHash']->label, $result['?passwordSalt']->label);
    102109            }
    103110        }
    104111        return $users;
    105112        }
     113
     114    public static function create($obj) {
     115        return new User($obj->uid, $obj->email,$obj->passwordHash,$obj->passwordSalt);
     116    }
     117
    106118}
    107119
  • Dev/branches/jos-branch/server/data

    • Property svn:ignore set to
      *
      questions
      results
      surveys
  • Dev/branches/jos-branch/server/rdfConstants.php

    r268 r285  
    3030define('UID',SURVEYTOOL_PREDICATES_NAMESPACE . 'uid');
    3131define('EMAIL',SURVEYTOOL_PREDICATES_NAMESPACE . 'email');
    32 define('PASSWORD',SURVEYTOOL_PREDICATES_NAMESPACE . 'password');
     32define('PASSWORDHASH',SURVEYTOOL_PREDICATES_NAMESPACE . 'passwordHash');
     33define('PASSWORDSALT',SURVEYTOOL_PREDICATES_NAMESPACE . 'passwordSalt');
    3334define('CREATOR',SURVEYTOOL_PREDICATES_NAMESPACE . 'creator');
    3435define('TITLE',SURVEYTOOL_PREDICATES_NAMESPACE . 'title');
Note: See TracChangeset for help on using the changeset viewer.