Changeset 882

Show
Ignore:
Timestamp:
02/24/08 22:15:24
Author:
goldoraf
Message:

More work on notifier demo app

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • apps/notifier/app/models/user.php

    r876 r882  
    77    public static function authenticate($login, $password) 
    88    { 
    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')); 
    1021    } 
    1122} 
  • apps/notifier/app/resources/base_resource.php

    r876 r882  
    55class BaseResource extends SResource 
    66{ 
     7    protected $authenticated_user; 
     8     
    79    protected $before_filters = array('authenticate'); 
    810     
    911    protected function authenticate() 
    1012    { 
    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); 
    1326    } 
    1427} 
  • apps/notifier/app/views/test/index.php

    r877 r882  
    88</head> 
    99<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" />&nbsp; 
     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;"> 
    1331        <div id="tabs"> 
    1432            <a href="#" class="active" onclick="notifier.ui.showTab(this, 'friends-timeline');">Timeline</a> 
     
    3149        </div> 
    3250    </div> 
     51</div> 
    3352</body> 
    3453</html> 
  • apps/notifier/conf/routes.php

    r844 r882  
    44$map->connect('api/notes/:id', array('resource' => 'notes')); 
    55$map->connect('api/public_timeline', array('resource' => 'public_timeline')); 
     6$map->connect('api/users', array('resource' => 'users')); 
    67$map->connect(':controller/:action/:id'); 
    78 
  • apps/notifier/public/js/notifier.js

    r877 r882  
    33notifier.prefs = {}; 
    44notifier.prefs.baseUrl = 'http://localhost/notifier/api/'; 
    5 notifier.prefs.email = 'test@test.com'; 
     5notifier.prefs.username = 'test@test.com'; 
    66notifier.prefs.password = 'test'; 
     7 
     8notifier.actions = {}; 
     9notifier.actions.login = function() { 
     10         
     11} 
     12notifier.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} 
    727 
    828notifier.ui = {}; 
    929notifier.ui.toggleLoading = function() { 
    10        $('#loading').toggle(); 
     30    $('#loading').toggle(); 
    1131} 
    1232notifier.ui.showTab = function(tabLink, tabName) { 
     
    1737} 
    1838notifier.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>' 
    2343} 
    2444notifier.ui.refreshFriendsTimeline = function(notes) { 
    25        $('#friends-timeline').empty(); 
     45    $('#friends-timeline').empty(); 
    2646    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   
    2949} 
    3050notifier.ui.prependNote = function(note) { 
    31        $('#friends-timeline').prepend(notifier.ui.htmlForNote(note)); 
     51    $('#friends-timeline').prepend(notifier.ui.htmlForNote(note)); 
    3252} 
    3353 
     
    3555notifier.api.doRequest = function(url, options) { 
    3656    var defaults = { 
    37                method: 'GET', 
    38                params: {}, 
    39                authenticate: false, 
    40                callback: null 
    41        }; 
    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   
    4666 
    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);            
    7698} 
    7799 
    78 notifier.api.sendNote = function(text, callback) { 
    79        return this.doRequest('notes.json', { 
    80                method: 'POST', 
    81                params: {'text': text}, 
    82                callback: callback 
    83        }); 
     100notifier.api.sendNote = function(text) { 
     101    return this.doRequest('notes.json', { 
     102        method: 'POST', 
     103        params: {'text': text}, 
     104        callback: callback 
     105    }); 
    84106} 
    85107 
    86108notifier.api.publicTimeline = function(callback) { 
    87        return this.doRequest('public_timeline.json', { 
    88                callback: callback 
    89        }); 
     109    return this.doRequest('public_timeline.json', { 
     110        callback: callback 
     111    }); 
    90112} 
  • apps/notifier/public/styles/main.css

    r877 r882  
    99    text-align: center; 
    1010} 
     11label { 
     12        display: block; 
     13} 
    1114textarea { 
    1215        width: 100%; 
    1316} 
    14 input
     17input[type='text'], input[type='password']
    1518        padding: 5px; 
     19        display: block; 
     20        margin-bottom: 10px; 
    1621} 
    1722#container {