- Timestamp:
- 02/24/12 10:03:02 (13 years ago)
- 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 46 46 47 47 $user = null; 48 $data = null;49 48 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']); 54 50 if (!$user) { 55 throw new ResponseException("Incorrect usernameand password", Response::UNAUTHORIZED);51 throw new ResponseException("Incorrect email and password", Response::UNAUTHORIZED); 56 52 } 57 53 set_session_cookie($response, $user); … … 59 55 $user = restore_session($response); 60 56 } 61 $response->body = json_encode($user); 57 $response->body = $user; 58 return $response; 59 } 60 61 } 62 63 /** 64 * @uri /register 65 */ 66 class 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; 62 79 return $response; 63 80 } … … 83 100 $type = $this->getType($request); 84 101 $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; 88 104 return $response; 89 105 } … … 94 110 95 111 $type = $this->getType($request); 96 $data = json_decode($request->data);97 112 $onlyAdd = $request->ifNoneMatch('*'); 98 113 $onlyUpdate = $request->ifMatch('*'); 99 114 100 115 $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)); 103 118 if (!empty($objects)) { 104 119 $object = $objects[0]; … … 116 131 $response->code = Response::OK; 117 132 } 118 foreach ($ data as $key => $val) {133 foreach ($request->data as $key => $val) { 119 134 $object->$key = $val; 120 135 } 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 127 148 } 128 149 … … 150 171 } 151 172 152 $response->body = json_encode($objects[0]);153 return $response; 154 } 155 156 function p ut($request) {173 $response->body = $objects[0]; 174 return $response; 175 } 176 177 function post($request) { 157 178 $response = new Response($request); 158 179 restore_session($response); 159 180 160 181 $info = $this->getTypeAndUid($request); 161 $data = json_decode($request->data);162 182 $onlyAdd = $request->ifNoneMatch('*'); 163 183 $onlyUpdate = $request->ifMatch('*'); … … 179 199 $response->code = Response::OK; 180 200 } 181 foreach ($ data as $key => $val) {201 foreach ($request->data as $key => $val) { 182 202 $object->$key = $val; 183 203 } 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); 188 214 } 189 215 190 216 function delete($request) { 191 restore_session( );217 restore_session(new Response($request)); 192 218 throw new ReponseException("Delete not implemented", Response::METHODNOTALLOWED); 193 219 } … … 202 228 $request->baseUri = $baseUri; 203 229 } 204 230 $request->data = Marshaller::unmarshall(json_decode($request->data)); 205 231 try { 206 232 $resource = $request->loadResource(); … … 208 234 } catch (ResponseException $e) { 209 235 $response = $e->response($request); 210 $response->body = json_encode(array('errorMsg' => $response->body));236 $response->body = array('errorMsg' => $response->body); 211 237 } catch (Exception $e) { 212 238 $response = new Response($request); 213 239 $response->code = Response::INTERNALSERVERERROR; 214 $response->body = json_encode(array('errorMsg' => "Unhandled exception: " . $e));240 $response->body = array('errorMsg' => "Unhandled exception: " . $e); 215 241 } 216 242 add_default_headers($response); 243 $response->body = json_encode(Marshaller::marshall($response->body)); 217 244 $response->output(); 245 218 246 ?> -
Dev/branches/jos-branch/server/classes/Auth.php
r256 r285 5 5 class Auth { 6 6 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."); 14 11 } 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; 16 28 } 17 29 18 30 public static function restore($session) { 19 31 $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."); 22 34 } 23 return false;35 return $user_results[0]; 24 36 } 25 37 -
Dev/branches/jos-branch/server/classes/models/Answer.php
r269 r285 18 18 public static function create($obj) 19 19 { 20 return new Answer($obj->uid, $obj->question, $obj->values) 20 return new Answer($obj->uid, $obj->question, $obj->values); 21 21 } 22 22 -
Dev/branches/jos-branch/server/classes/models/AnswerSet.php
r268 r285 187 187 return $answers; 188 188 } 189 190 public static function create($obj) { 191 return new AnswerSet($obj->uid, $obj->survey, $obj->respondent, $obj->datetime, $obj->answers); 192 } 193 189 194 } 190 195 -
Dev/branches/jos-branch/server/classes/models/Application.php
r256 r285 113 113 return $applications; 114 114 } 115 116 public static function create($obj) { 117 return new Application($obj->uid, $obj->title, $obj->description, $obj->style); 118 } 115 119 } 116 120 -
Dev/branches/jos-branch/server/classes/models/ApplicationInstance.php
r263 r285 167 167 168 168 } 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 } 169 175 } 170 176 ?> -
Dev/branches/jos-branch/server/classes/models/ResearchToolObject.php
r256 r285 11 11 12 12 13 abstract class ResearchToolObject {13 abstract class ResearchToolObject implements ResearchToolObjectInterface { 14 14 15 public $uid ;15 public $uid = null; 16 16 17 public function getUid() { 18 return $this->uid; 19 } 20 17 21 public function evaluate() 18 22 { … … 79 83 } 80 84 85 interface ResearchToolObjectInterface { 86 function getUid(); 87 static function get($arguments); 88 static function create($obj); 89 function evaluate(); 90 function save(); 91 } 92 81 93 ?> -
Dev/branches/jos-branch/server/classes/models/Respondent.php
r256 r285 16 16 17 17 public $email; 18 public $password; 18 public $passwordHash; 19 public $passwordSalt; 19 20 20 21 /** … … 24 25 * @param type password 25 26 */ 26 public function __construct($uid = null, $email = null, $password = null)27 public function __construct($uid = null, $email = null, $passwordHash = null, $passwordSalt = null) 27 28 { 28 29 if(!isset($uid)) … … 33 34 $this->uid = $uid; 34 35 $this->email = $email; 35 $this->password = $password; 36 $this->passwordHash = $passwordHash; 37 $this->passwordSalt = $passwordSalt; 36 38 } 37 39 /** … … 64 66 $model->add(new Statement($resourceRespondent,$predicateName,$literalRespondentName)); 65 67 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 69 76 $model->saveAs(Respondent::$filename, 'rdf'); 70 77 return true; … … 85 92 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 86 93 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 87 SELECT ?uid, ?email, ?password 94 SELECT ?uid, ?email, ?passwordHash, ?passwordSalt 88 95 WHERE 89 96 { … … 91 98 predicates:uid ?uid ; 92 99 predicates:email ?email ; 93 predicates:password ?password ; 100 predicates:passwordHash ?passwordHash ; 101 predicates:passwordSalt ?passwordSalt ; 94 102 ' . ResearchToolObject::createArguments($arguments) . ' 95 103 }'; … … 102 110 foreach($results as $result) 103 111 { 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); 105 113 } 106 114 } 107 115 return $respondents; 108 116 } 117 118 public static function create($obj) { 119 return new Respondent($obj->uid, $obj->email, $obj->passwordHash, $obj->passwordSalt); 120 } 121 109 122 } 110 123 111 112 113 114 124 ?> -
Dev/branches/jos-branch/server/classes/models/ResultSet.php
r263 r285 158 158 return $answersets; 159 159 } 160 161 public static function create($obj) { 162 return new ResultSet($obj->uid, $obj->answersets, $obj->playerresults, 163 $obj->groupresults, $obj->periodicresults); 164 } 160 165 } -
Dev/branches/jos-branch/server/classes/models/Session.php
r263 r285 24 24 * @param type $title 25 25 * @param type $creator 26 * @param type $ datetime26 * @param type $creationdate 27 27 * @param type $pipeline 28 28 */ 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) 30 30 { 31 31 if(!isset($uid)) … … 36 36 $this->title = $title; 37 37 $this->creator = $creator; 38 $this->creationdate = $ datetime;38 $this->creationdate = $creationdate; 39 39 $this->pipeline = $pipeline; 40 40 } … … 141 141 } 142 142 } 143 144 $model->saveAs(Session::$filename, 'rdf'); 145 return true; 143 144 return $model->saveAs(Session::$filename, 'rdf'); 146 145 } 147 146 … … 320 319 } 321 320 321 public static function create($obj) { 322 return new Session($obj->uid, $obj->title, $obj->creator, 323 $obj->creationdate, $obj->pipeline); 324 } 325 322 326 } 323 327 324 325 328 ?> -
Dev/branches/jos-branch/server/classes/models/SessionInstance.php
r263 r285 235 235 } 236 236 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 } 237 242 238 243 } 244 245 ?> -
Dev/branches/jos-branch/server/classes/models/Survey.php
r263 r285 169 169 } 170 170 171 public static function create($obj) { 172 return new Survey($obj->uid, $obj->title, $obj->description, 173 $obj->creator, $obj->questions); 174 } 171 175 172 176 } -
Dev/branches/jos-branch/server/classes/models/SurveyInstance.php
r269 r285 26 26 * @param type $answersets: A list of answersets. 27 27 */ 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) 29 29 { 30 30 if(!isset($uid)) -
Dev/branches/jos-branch/server/classes/models/User.php
r256 r285 17 17 18 18 public $email; 19 public $password; 19 public $passwordHash; 20 public $passwordSalt; 20 21 21 22 /** … … 23 24 * If the user does not yet exist in the database, call with null as first parameter 24 25 */ 25 public function __construct($uid = null, $email = null, $password = null) {26 public function __construct($uid = null, $email = null, $passwordHash = null, $passwordSalt = null) { 26 27 if(!isset($uid)) 27 28 { … … 30 31 $this->uid = $uid; 31 32 $this->email = $email; 32 $this->password = sha1($password); 33 $this->passwordHash = $passwordHash; 34 $this->passwordSalt = $passwordSalt; 33 35 } 34 36 … … 60 62 $model->add(new Statement($resourceUser,$predicateName,$literalUserName)); 61 63 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)); 65 71 66 72 $model->saveAs(User::$filename, 'rdf'); … … 82 88 PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> 83 89 PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> 84 SELECT ?uid, ?email, ?password 90 SELECT ?uid, ?email, ?passwordHash, ?passwordSalt 85 91 WHERE 86 92 { … … 88 94 predicates:uid ?uid ; 89 95 predicates:email ?email ; 90 predicates:password ?password ; 96 predicates:passwordHash ?passwordHash ; 97 predicates:passwordSalt ?passwordSalt ; 91 98 ' . ResearchToolObject::createArguments($arguments) . ' 92 99 }'; … … 99 106 foreach($results as $result) 100 107 { 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); 102 109 } 103 110 } 104 111 return $users; 105 112 } 113 114 public static function create($obj) { 115 return new User($obj->uid, $obj->email,$obj->passwordHash,$obj->passwordSalt); 116 } 117 106 118 } 107 119 -
Dev/branches/jos-branch/server/data
-
Property
svn:ignore
set to
*
questions
results
surveys
-
Property
svn:ignore
set to
-
Dev/branches/jos-branch/server/rdfConstants.php
r268 r285 30 30 define('UID',SURVEYTOOL_PREDICATES_NAMESPACE . 'uid'); 31 31 define('EMAIL',SURVEYTOOL_PREDICATES_NAMESPACE . 'email'); 32 define('PASSWORD',SURVEYTOOL_PREDICATES_NAMESPACE . 'password'); 32 define('PASSWORDHASH',SURVEYTOOL_PREDICATES_NAMESPACE . 'passwordHash'); 33 define('PASSWORDSALT',SURVEYTOOL_PREDICATES_NAMESPACE . 'passwordSalt'); 33 34 define('CREATOR',SURVEYTOOL_PREDICATES_NAMESPACE . 'creator'); 34 35 define('TITLE',SURVEYTOOL_PREDICATES_NAMESPACE . 'title');
Note: See TracChangeset
for help on using the changeset viewer.