4 * This file is (c) Copyright 2010 by Olivier BERGER, Madhumita DHAR, Institut TELECOM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * This program has been developed in the frame of the COCLICO
21 * project with financial support of its funders.
25 require_once 'token_api.php';
28 * OAuth Access Token concrete class
30 * Extends the OauthAuthzToken which already contains all needed attributes.
32 * @author Olivier Berger
35 class OauthAuthzAccessToken extends OauthAuthzToken {
37 const TOKEN_TYPE = 'access';
41 * @param int $p_consumer_id
42 * @param string $p_key
43 * @param string $p_secret
44 * @param int $p_user_id
45 * @param int $p_time_stamp
47 function __construct( $p_consumer_id, $p_key, $p_secret, $p_user_id=null, $p_role_id, $p_time_stamp=null) {
48 parent::__construct($p_consumer_id, $p_key, $p_secret, $p_user_id, $p_time_stamp);
50 $this->role_id = $p_role_id;
54 * Converts a row returned by select * into an object
57 * @return OauthAuthzRequestToken
59 static function row_to_new_token ($t_row) {
60 $t_token = new OauthAuthzAccessToken( $t_row['consumer_id'], $t_row['token_key'], $t_row['token_secret'], $t_row['user_id'], $t_row['role_id'], $t_row['time_stamp'] );
61 $t_token->id = $t_row['id'];
65 static function load( $p_id ) {
66 $row = parent::load($p_id, self::TOKEN_TYPE);
67 return self::row_to_new_token($row);
70 static function load_all($user_id=null) {
71 $rows = parent::load_all($user_id, self::TOKEN_TYPE);
74 foreach ($rows as $row) {
75 $token = self::row_to_new_token($row);
83 static function load_by_key( $p_token_key ) {
84 $row = parent::load_by_key($p_token_key, self::TOKEN_TYPE);
85 return self::row_to_new_token($row);
89 parent::delete(self::TOKEN_TYPE);
93 * Loads tokens related to a particular consumer (and a particular user, if specified)
95 * @param int $consumer_id
96 * @param int $user_id (may be null)
97 * @return array of OauthAuthzAccessToken
99 static function load_by_consumer($consumer_id, $user_id=null) {
101 $DBSTORE = FFDbOAuthDataStore::singleton();
103 // this is a hack to retrieve the table name from the base class
104 $t_rows = $DBSTORE->find_tokens_by_consumer(self::TOKEN_TYPE, $consumer_id, $user_id);
108 foreach ($t_rows as $t_row) {
109 $t_token = self::row_to_new_token($t_row);
111 $t_tokens[] = $t_token;
118 public function getRoleId() {
119 return $this->role_id;
124 * Check that mandatory values are OK
126 function check_mandatory() {
127 parent::check_mandatory();
129 // all access tokens should be on behalf of a user
130 if ( strlen(trim( $this->user_id ))==0 ) {
131 throw new OAuthException('Mandatory "user_id" Access Token attribute not set.');
136 * Insert or update the token into the DB
140 $this->check_mandatory();
142 $DBSTORE = FFDbOAuthDataStore::singleton();
143 $this->id = $DBSTORE->save_access_token($this);