Skip to content

Commit

Permalink
Refactored user management. Filez has its own user database now.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudD committed Apr 28, 2011
1 parent ad60468 commit 68d605e
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 65 deletions.
9 changes: 6 additions & 3 deletions app/controllers/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function checkFilesAction () {
// Send mail for files which will be deleted in less than 2 days
$days = fz_config_get('cron', 'days_before_expiration_mail');
foreach (Fz_Db::getTable('File')->findFilesToBeDeleted ($days) as $file) {
// TODO improve the SQL command to retrieve uploader email at the same time
// to reduce the # of request made by notifyDeletionByEmail
if ($file->notify_uploader) {
$file->del_notif_sent = true;
$file->save ();
Expand All @@ -65,6 +67,7 @@ public function checkFilesAction () {
private function notifyDeletionByEmail (App_Model_File $file) {
try {
$mail = $this->createMail();
$user = $file->getUploader ();
$subject = __r('[FileZ] Your file "%file_name%" is going to be deleted', array (
'file_name' => $file->file_name));
$msg = __r('email_delete_notif (%file_name%, %file_url%, %filez_url%, %available_until%)', array(
Expand All @@ -75,13 +78,13 @@ private function notifyDeletionByEmail (App_Model_File $file) {
));
$mail->setBodyText ($msg);
$mail->setSubject ($subject);
$mail->addTo ($file->uploader_email);
$mail->addTo ($user->email);
$mail->send ();

fz_log ('Delete notification sent to '.$file->uploader_email, FZ_LOG_CRON);
fz_log ('Delete notification sent to '.$user->email, FZ_LOG_CRON);
}
catch (Exception $e) {
fz_log ('Can\'t send email to '.$file->uploader_email
fz_log ('Can\'t send email to '.$user->email
.' file_id:'.$file->id, FZ_LOG_CRON_ERROR);
}
}
Expand Down
12 changes: 5 additions & 7 deletions app/controllers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,18 @@ public function emailAction () {
$user = $this->getUser ();
$mail = $this->createMail();
$subject = __r('[FileZ] "%sender%" wants to share a file with you', array (
'sender' => $user['firstname'].' '.$user['lastname']));
'sender' => $user));
$msg = __r('email_share_file (%file_name%, %file_url%, %sender%, %msg%)', array(
'file_name' => $file->file_name,
'file_url' => $file->getDownloadUrl(),
'msg' => $_POST ['msg'],
'sender' => $user['firstname'].' '.$user['lastname'],
'sender' => $user,
));
$mail->setBodyText ($msg);
$mail->setSubject ($subject);
$mail->setReplyTo ($user['email'],
$user['firstname'].' '.$user['lastname']);
$mail->setReplyTo ($user->email, $user);
$mail->clearFrom();
$mail->setFrom ($user['email'],
$user['firstname'].' '.$user['lastname']);
$mail->setFrom ($user->email, $user);

$emailValidator = new Zend_Validate_EmailAddress();
foreach (explode (' ', $_POST['to']) as $email) {
Expand Down Expand Up @@ -284,7 +282,7 @@ protected function sendFile (App_Model_File $file, $forceDownload = true) {
* Checks if the user is the owner of the file. Stop the request if not.
*
* @param App_Model_File $file
* @param array $user
* @param App_Model_User $user
*/
protected function checkOwner (App_Model_File $file, $user) {
if ($file->isOwner ($user))
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function indexAction () {
set ('start_from' , Zend_Date::now ()->get (Zend_Date::DATE_SHORT));
set ('refresh_rate' , 1200);
set ('files' , Fz_Db::getTable ('File')
->findByOwnerOrderByUploadDateDesc ($user['id']));
->findByOwnerOrderByUploadDateDesc ($user));
set ('use_progress_bar' , $progressMonitor->isInstalled ());
set ('upload_id_name' , $progressMonitor->getUploadIdName ());
set ('free_space_left' , $freeSpaceLeft);
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ private function saveFile ($post, $uploadedFile) {
$file->save ();

if ($file->moveUploadedFile ($uploadedFile)) {
fz_log ('Saved "'.$file->file_name.'"['.$file->id.'] uploaded by '.$user['email']);
fz_log ('Saved "'.$file->file_name.'"['.$file->id.'] uploaded by '.$user);
return $file;
}
else {
$file->delete ();
return null;
}
} catch (Exception $e) {
fz_log ('Can\'t save file "'.$uploadedFile['name'].'" uploaded by '.$user['email'], FZ_LOG_ERROR);
fz_log ('Can\'t save file "'.$uploadedFile['name'].'" uploaded by '.$user, FZ_LOG_ERROR);
fz_log ($e, FZ_LOG_ERROR);
return null;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ private function sendFileUploadedMail (App_Model_File $file) {
$mail = $this->createMail();
$mail->setBodyText ($msg);
$mail->setSubject ($subject);
$mail->addTo ($user ['email'], $user['firstname'].' '.$user['lastname']);
$mail->addTo ($user->email, $user);

try {
$mail->send ();
Expand Down
21 changes: 10 additions & 11 deletions app/models/DbTable/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ class App_Model_DbTable_File extends Fz_Db_Table_Abstract {
protected $_columns = array (
'del_notif_sent',
'file_name',
'uploader_email',
'file_size',
'nom_physique',
'available_from',
'available_until',
'download_count',
'notify_uploader',
'uploader_uid',
'created_by',
'created_at',
'extends_count',
'comment',
'created_at',
'password',
);

Expand Down Expand Up @@ -116,15 +115,15 @@ public function findByFzOneHash ($hash) {
/**
* Return all file owned by $uid which are available (not deleted)
*
* @param string $uid
* @param App_Model_User $user
* @return array of App_Model_File
*/
public function findByOwnerOrderByUploadDateDesc ($uid) {
public function findByOwnerOrderByUploadDateDesc ($user) {
$sql = 'SELECT * FROM '.$this->getTableName ()
.' WHERE uploader_uid=:uid '
.' WHERE created_by=:id '
.' AND available_until >= CURRENT_DATE() '
.' ORDER BY created_at DESC';
return $this->findBySql ($sql, array (':uid' => $uid));
return $this->findBySql ($sql, array (':id' => $user->id));
}

/**
Expand Down Expand Up @@ -164,23 +163,23 @@ public function findFilesToBeDeleted ($days = 2) {
/**
* Return disk space used by someone
*
* @param array $user User data
* @param App_Model_User $user User
* @return float Size in bytes
*/
public function getTotalDiskSpaceByUser ($user) {
$result = option ('db_conn')
->prepare ('SELECT sum(file_size) FROM `'
.$this->getTableName ()
.'` WHERE uploader_email = ?'
.'` WHERE created_by = ?'
.' AND available_until >= CURRENT_DATE() ');
$result->execute (array ($user['email']));
$result->execute (array ($user->id));
return (float) $result->fetchColumn ();
}

/**
* Return remaining disk space available for user $user
*
* @param array $user User data
* @param App_Model_User $user User data
* @return float Size in bytes or string if $shorthand = true
*/
public function getRemainingSpaceForUser ($user) {
Expand Down
52 changes: 52 additions & 0 deletions app/models/DbTable/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2010 Université d'Avignon et des Pays de Vaucluse
* email: gpl@univ-avignon.fr
*
* This file is part of Filez.
*
* Filez is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Filez is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Filez. If not, see <http://www.gnu.org/licenses/>.
*/

class App_Model_DbTable_User extends Fz_Db_Table_Abstract {

protected $_rowClass = 'App_Model_User';
protected $_name = 'fz_user';
protected $_columns = array (
'id',
'username',
'password',
'salt',
'firstname',
'lastname',
'email',
'is_admin',
'created_at',
);

/**
* Retrieve a user by its username
*
* @param string $username
* @return App_Model_User or null if not found
*/
public function findByUsername ($username) {
$sql = 'SELECT * FROM '.$this->getTableName ().' WHERE username = ?';
return $this->findOneBySQL ($sql, $username);
}

}



28 changes: 10 additions & 18 deletions app/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
/**
* @property boolean $del_notif_sent
* @property string $file_name
* @property string $uploader_email
* @property int $file_size
* @property string $available_from DATE
* @property string $available_until DATE
* @property int $download_count
* @property string $comment
* @property boolean $notify_uploader
* @property string $uploader_uid
* @property int $extends_count
* @property int $created_by
* @property int $created_at TIMESTAMP
* @property int $extends_count
* @property string $password
*/
class App_Model_File extends Fz_Db_Table_Row_Abstract {
Expand Down Expand Up @@ -176,35 +175,28 @@ public function setFileInfo (array $file) {
* Set the uploader of the file from an associative array containing
* 'id' & 'email' keys.
*
* @param array $user
* @param App_Model_User $user
*/
public function setUploader (array $user) {
$this->uploader_uid = $user ['id'];
$this->uploader_email = $user ['email'];
public function setUploader (App_Model_User $user) {
$this->created_by = $user->id;
}
/**
* Return file uploader info
*
* @return array $user
* @return App_Model_User $user
*/
public function getUploader () {
return option ('userFactory')->findById ($this->uploader_uid);

// TODO retrieve user from database if he has been invited
return Fz_Db::getTable('User')->findById ($this->created_by);
}

/**
* Checks if the user passed is the owner of the file
*
* @param array $user
* @param App_Model_User $user
* @return boolean
*/
public function isOwner ($user) {
return is_array ($user) && (
(array_key_exists ('email', $user) // check for invited users
&& $this->uploader_email == $user ['email'])
|| (array_key_exists ('id', $user) // or registered users
&& $this->uploader_uid == $user ['id']));
public function isOwner (App_Model_User $user) {
return ($this->created_by === $user->id);
}

/**
Expand Down
67 changes: 67 additions & 0 deletions app/models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2010 Université d'Avignon et des Pays de Vaucluse
* email: gpl@univ-avignon.fr
*
* This file is part of Filez.
*
* Filez is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Filez is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Filez. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @password int $id
* @password string $username
* @password string $password
* @password string $salt
* @password string $firstname
* @password string $lastname
* @password string $email
* @password boolean $is_admin
* @property int $created_at TIMESTAMP
*/
class App_Model_User extends Fz_Db_Table_Row_Abstract {

protected $_tableClass = 'App_Model_DbTable_User';

/**
* Constructor
*
* @param boolean $exists Whether the object exists in database or not.
* If false a ID will be automatically choosen
*/
public function __construct ($exists = false) {
parent::__construct ($exists);
}

/**
* Return the string representation of the file object (file name)
* @return string
*/
public function __toString () {
return $this->firstname.' '.$this->lastname;
}

/**
* Return every file uploaded by the user (
*
* @param boolean $expired Are the expired file included ?
* @return array Array of App_Model_File
*/
public function getFiles ($includeExpired = false) {
return Fz_Db::getTable('File')->findByOwnerOrderByUploadDateDesc ($this);
// TODO handle the $includeExpired parameter
}

}

8 changes: 1 addition & 7 deletions app/views/file/preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
</p>

<p id="owner">
<?php echo __('Uploaded by') ?> : <b>
<?php if (array_key_exists('firstname', $uploader)): ?>
<?php echo h($uploader['firstname']).' '.h($uploader['lastname']) ?>
<?php else: ?>
<?php echo h($uploader['email']) ?>
<?php endif ?>
</b>
<?php echo __('Uploaded by') ?> : <b><?php echo h($uploader) ?></b>
</p>

<?php if ($file->comment): ?>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layout/_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

<?php if (isset ($user)): ?>
<p id="auth-box">
<?php echo $user ['email'] ?> |
<?php echo $user->email ?> |
<a href="<?php echo url_for ('/logout') ?>" title="<?php echo __('Log out') ?>">&nbsp;</a>
</p>
<?php endif ?>
</header>
</header>
Loading

0 comments on commit 68d605e

Please sign in to comment.