Changeset 882
- Timestamp:
- 02/24/08 22:15:24
- Files:
-
- apps/notifier/app/models/user.php (modified) (1 diff)
- apps/notifier/app/resources/base_resource.php (modified) (1 diff)
- apps/notifier/app/resources/users_resource.php (added)
- apps/notifier/app/views/test/index.php (modified) (2 diffs)
- apps/notifier/conf/routes.php (modified) (1 diff)
- apps/notifier/public/js/notifier.js (modified) (3 diffs)
- apps/notifier/public/styles/main.css (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
apps/notifier/app/models/user.php
r876 r882 7 7 public static function authenticate($login, $password) 8 8 { 9 return true; 9 try { 10 return User::$objects->get('name = ?', array($login, md5($password))); 11 } catch (SRecordNotFound $e) { 12 return false; 13 } 14 } 15 16 public function validate() 17 { 18 $this->validate_presence_of(array('name', 'email', 'pwd'), 19 array('message' => 'est obligatoire')); 20 $this->validate_uniqueness_of('name', array('message' => 'est déjà pris')); 10 21 } 11 22 } apps/notifier/app/resources/base_resource.php
r876 r882 5 5 class BaseResource extends SResource 6 6 { 7 protected $authenticated_user; 8 7 9 protected $before_filters = array('authenticate'); 8 10 9 11 protected function authenticate() 10 12 { 11 return SBasicHttpAuthentication::authenticate_or_request($this->request, $this->response, 12 array('User', 'authenticate'), 'Notifier'); 13 $this->authenticated_user 14 = SBasicHttpAuthentication::authenticate_or_request( 15 $this->request, $this->response, array('User', 'authenticate'), 'Notifier'); 16 } 17 18 protected function must_specifiy_user() 19 { 20 $this->user = User::$objects->get_by_name($this->params['username']); 21 } 22 23 protected function specified_user_must_be_authenticated_user() 24 { 25 return ($this->authenticated_user == $this->user); 13 26 } 14 27 } apps/notifier/app/views/test/index.php
r877 r882 8 8 </head> 9 9 <body> 10 <div id="container"> 11 <h1>Notifier : test de l'API</h1> 12 <p id="loading" style="display:none;">Chargement</p> 10 <div id="container"> 11 <h1>Notifier : test de l'API</h1> 12 <p id="loading" style="display:none;">Chargement</p> 13 <div id="login"> 14 <label for="login">Identifiant</label> 15 <input type="text" id="login" value="" /> 16 <label for="pwd">Mot de passe</label> 17 <input type="password" id="pwd" value="" /> 18 <input type="submit" onclick="notifier.actions.login();" value="Connexion" /> 19 <a href="#" onclick="$('#login').hide();$('#register').show();">S'enregistrer</a> 20 </div> 21 <div id="register" style="display:none;"> 22 <label for="reg_email">Email</label> 23 <input type="text" id="reg_email" value="" /> 24 <label for="reg_login">Identifiant</label> 25 <input type="text" id="reg_login" value="" /> 26 <label for="reg_pwd">Mot de passe</label> 27 <input type="password" id="reg_pwd" value="" /> 28 <input type="submit" onclick="notifier.actions.register();" value="Créer un compte" /> 29 </div> 30 <div id="main" style="display:none;"> 13 31 <div id="tabs"> 14 32 <a href="#" class="active" onclick="notifier.ui.showTab(this, 'friends-timeline');">Timeline</a> … … 31 49 </div> 32 50 </div> 51 </div> 33 52 </body> 34 53 </html> apps/notifier/conf/routes.php
r844 r882 4 4 $map->connect('api/notes/:id', array('resource' => 'notes')); 5 5 $map->connect('api/public_timeline', array('resource' => 'public_timeline')); 6 $map->connect('api/users', array('resource' => 'users')); 6 7 $map->connect(':controller/:action/:id'); 7 8 apps/notifier/public/js/notifier.js
r877 r882 3 3 notifier.prefs = {}; 4 4 notifier.prefs.baseUrl = 'http://localhost/notifier/api/'; 5 notifier.prefs. email= 'test@test.com';5 notifier.prefs.username = 'test@test.com'; 6 6 notifier.prefs.password = 'test'; 7 8 notifier.actions = {}; 9 notifier.actions.login = function() { 10 11 } 12 notifier.actions.register = function() { 13 var data = { 14 'user[name]': $('#reg_login').val(), 15 'user[email]': $('#reg_email').val(), 16 'user[pwd]': $('#reg_pwd').val() 17 }; 18 notifier.api.doRequest('users.json', { 19 method: 'POST', 20 params: data, 21 success: function() { 22 $('#register').hide(); 23 $('#login').show(); 24 } 25 }); 26 } 7 27 8 28 notifier.ui = {}; 9 29 notifier.ui.toggleLoading = function() { 10 $('#loading').toggle();30 $('#loading').toggle(); 11 31 } 12 32 notifier.ui.showTab = function(tabLink, tabName) { … … 17 37 } 18 38 notifier.ui.htmlForNote = function(note) { 19 return '<div class="note">'20 +'<p class="note-text">'+note.text+'</p>'21 +'<p class="note-details">'+'raphael'+' le '+note.timestamp+'</p>'22 +'</div>'39 return '<div class="note">' 40 +'<p class="note-text">'+note.text+'</p>' 41 +'<p class="note-details">'+'raphael'+' le '+note.timestamp+'</p>' 42 +'</div>' 23 43 } 24 44 notifier.ui.refreshFriendsTimeline = function(notes) { 25 $('#friends-timeline').empty();45 $('#friends-timeline').empty(); 26 46 for (var i in notes) { 27 $('#friends-timeline').append(notifier.ui.htmlForNote(notes[i]));28 }47 $('#friends-timeline').append(notifier.ui.htmlForNote(notes[i])); 48 } 29 49 } 30 50 notifier.ui.prependNote = function(note) { 31 $('#friends-timeline').prepend(notifier.ui.htmlForNote(note));51 $('#friends-timeline').prepend(notifier.ui.htmlForNote(note)); 32 52 } 33 53 … … 35 55 notifier.api.doRequest = function(url, options) { 36 56 var defaults = { 37 method: 'GET',38 params: {},39 authenticate: false,40 callback: null41 };42 if (options == null) options = {};43 for (var index in defaults) {44 if (typeof options[index] == "undefined") options[index] = defaults[index];45 }57 method: 'GET', 58 params: {}, 59 authenticate: false, 60 success: null 61 }; 62 if (options == null) options = {}; 63 for (var index in defaults) { 64 if (typeof options[index] == "undefined") options[index] = defaults[index]; 65 } 46 66 47 var reqParams = { 48 type: options.method, 49 data: options.params, 50 url: notifier.prefs.baseUrl + url, 51 beforeSend: function(xhr) { 52 notifier.ui.toggleLoading(); 53 if (options.authenticate) { 54 xhr.setRequestHeader("Authorization", 55 "Basic " + Base64.encode(notifier.prefs.email + ":" + notifier.prefs.password)); 56 xhr.setRequestHeader("Cookie", ""); 57 } 58 return xhr; 59 }, 60 complete: function (xhr, status) { 61 notifier.ui.toggleLoading(); 62 //alert(xhr.responseText); 63 }, 64 success: function (responseText, status) { 65 try { 66 var data = eval('(' + responseText + ')'); 67 } catch (e) { 68 var data = null; 69 } 70 if (options.callback !== null) { 71 options.callback(data); 72 } 73 } 74 } 75 return $.ajax(reqParams); 67 var reqParams = { 68 type: options.method, 69 data: options.params, 70 url: notifier.prefs.baseUrl + url, 71 beforeSend: function(xhr) { 72 notifier.ui.toggleLoading(); 73 if (options.authenticate) { 74 xhr.setRequestHeader("Authorization", 75 "Basic " + Base64.encode(notifier.prefs.email + ":" + notifier.prefs.password)); 76 xhr.setRequestHeader("Cookie", ""); 77 } 78 return xhr; 79 }, 80 complete: function (xhr, status) { 81 notifier.ui.toggleLoading(); 82 }, 83 success: function (responseText, status) { 84 try { 85 var data = eval('(' + responseText + ')'); 86 } catch (e) { 87 var data = null; 88 } 89 if (options.callback !== null) { 90 options.callback(data, status); 91 } 92 }, 93 error: function (xhr, status, error) { 94 alert("Erreur "+xhr.status+" !\n\n"+error); 95 } 96 } 97 return $.ajax(reqParams); 76 98 } 77 99 78 notifier.api.sendNote = function(text , callback) {79 return this.doRequest('notes.json', {80 method: 'POST',81 params: {'text': text},82 callback: callback83 });100 notifier.api.sendNote = function(text) { 101 return this.doRequest('notes.json', { 102 method: 'POST', 103 params: {'text': text}, 104 callback: callback 105 }); 84 106 } 85 107 86 108 notifier.api.publicTimeline = function(callback) { 87 return this.doRequest('public_timeline.json', {88 callback: callback89 });109 return this.doRequest('public_timeline.json', { 110 callback: callback 111 }); 90 112 } apps/notifier/public/styles/main.css
r877 r882 9 9 text-align: center; 10 10 } 11 label { 12 display: block; 13 } 11 14 textarea { 12 15 width: 100%; 13 16 } 14 input {17 input[type='text'], input[type='password'] { 15 18 padding: 5px; 19 display: block; 20 margin-bottom: 10px; 16 21 } 17 22 #container {
