1
0

Merge branch 'master' of github.com:rledisez/gallery3-contrib

This commit is contained in:
Romain LE DISEZ 2010-11-28 16:23:59 +01:00
commit 97696d2cb7
7 changed files with 138 additions and 112 deletions

View File

@ -21,22 +21,27 @@
class access extends access_Core { class access extends access_Core {
/** /**
* Does the active user have this permission on this item? * If the user is chrooted, deny access outside of the chroot.
* */
* @param string $perm_name static function user_can($user, $perm_name, $item) {
* @param Item_Model $item if( $user->id == identity::active_user()->id && user_chroot::album() ) {
* @return boolean if( $item->left_ptr < user_chroot::album()->left_ptr || user_chroot::album()->right_ptr < $item->right_ptr ) {
return false;
}
}
return parent::user_can($user, $perm_name, $item);
}
/**
* Copied from modules/gallery/helpers/access.php because of the usage of self::
*/ */
static function can($perm_name, $item) { static function can($perm_name, $item) {
return self::user_can(identity::active_user(), $perm_name, $item); return self::user_can(identity::active_user(), $perm_name, $item);
} }
/** /**
* If the active user does not have this permission, failed with an access::forbidden(). * Copied from modules/gallery/helpers/access.php because of the usage of self::
*
* @param string $perm_name
* @param Item_Model $item
* @return boolean
*/ */
static function required($perm_name, $item) { static function required($perm_name, $item) {
if (!self::can($perm_name, $item)) { if (!self::can($perm_name, $item)) {
@ -48,14 +53,4 @@ class access extends access_Core {
} }
} }
} }
static function user_can($user, $perm_name, $item) {
if( $user->id == identity::active_user()->id && user_chroot::album() ) {
if( $item->left_ptr < user_chroot::album()->left_ptr || user_chroot::album()->right_ptr < $item->right_ptr ) {
return false;
}
}
return parent::user_can($user, $perm_name, $item);
}
} }

View File

@ -24,11 +24,17 @@ class item extends item_Core {
if( user_chroot::album() ) { if( user_chroot::album() ) {
$model->and_open() $model->and_open()
->and_where("items.left_ptr", ">=", user_chroot::album()->left_ptr) ->and_where('items.left_ptr', '>=', user_chroot::album()->left_ptr)
->and_where("items.right_ptr", "<=", user_chroot::album()->right_ptr) ->and_where('items.right_ptr', '<=', user_chroot::album()->right_ptr)
->close(); ->close();
} }
return $model; return $model;
} }
static function root() {
return ( user_chroot::album() )
? user_chroot::album()
: parent::root();
}
} }

View File

@ -18,25 +18,40 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
// /!\ Hack: There is no good way to extend gallery url /*
* /!\ Hack
* Module 'gallery' already provides a class 'url' which extends url_Core. This
* hack renames class 'url', so user_chroot can have its own (which extends the
* original)
*/
$gallery_url = file_get_contents(MODPATH . 'gallery/helpers/MY_url.php'); $gallery_url = file_get_contents(MODPATH . 'gallery/helpers/MY_url.php');
$gallery_url = preg_replace('#^<\?php #', '', $gallery_url); $gallery_url = str_replace('<?php ', '', $gallery_url);
$gallery_url = preg_replace('#class url extends url_Core#', 'class url_G3 extends url_Core', $gallery_url); $gallery_url = str_replace('class url extends url_Core', 'class url_G3 extends url_Core', $gallery_url);
eval($gallery_url); eval($gallery_url);
class url extends url_G3 { class url extends url_G3 {
/**
* Add the chroot path at the begining of the requested URI
*/
static function parse_url() { static function parse_url() {
if( user_chroot::album() ) { if( user_chroot::album() ) {
if( Router::$current_uri == '' ) { if( Router::$controller == 'albums' && Router::$current_uri == '' ) {
Router::$controller = false; // Root album requested
} Router::$controller = null;
Router::$current_uri = trim(user_chroot::album()->relative_url().'/'.Router::$current_uri, '/');
Router::$current_uri = trim(user_chroot::album()->relative_url().'/'.Router::$current_uri, '/'); } else if( is_null(Router::$controller) && Router::$current_uri != '' ) {
// Non-root album requested
Router::$current_uri = trim(user_chroot::album()->relative_url().'/'.Router::$current_uri, '/');
}
} }
return parent::parse_url(); return parent::parse_url();
} }
/**
* Remove the chroot part of the URI.
*/
static function site($uri = '', $protocol = FALSE) { static function site($uri = '', $protocol = FALSE) {
if( user_chroot::album() ) { if( user_chroot::album() ) {
$uri = preg_replace('#^'.user_chroot::album()->relative_url().'#', '', $uri); $uri = preg_replace('#^'.user_chroot::album()->relative_url().'#', '', $uri);

View File

@ -21,18 +21,21 @@
class user_chroot_Core { class user_chroot_Core {
private static $_album = null; private static $_album = null;
/**
* Return the root album of the current user, or false if the user is not
* chrooted.
*/
public static function album() { public static function album() {
if( is_null(self::$_album) ) { if( is_null(self::$_album) ) {
self::$_album = false; self::$_album = false;
$user = identity::active_user(); $item = ORM::factory('item')
->join('user_chroots', 'items.id', 'user_chroots.album_id')
->where('user_chroots.id', '=', identity::active_user()->id)
->find();
$user_chroot = ORM::factory("user_chroot", $user->id); if( $item->loaded() ) {
if( $user_chroot->loaded() && $user_chroot->album_id != 0 ) { self::$_album = $item;
$item = ORM::factory("item", $user_chroot->album_id);
if( $item->loaded() ) {
self::$_album = $item;
}
} }
} }

View File

@ -21,92 +21,94 @@
class user_chroot_event_Core { class user_chroot_event_Core {
/** /**
* Called just before a user is deleted. This will remove the user from * Called just before a user deletion.
* the user_chroot directory.
*/ */
static function user_before_delete($user) { public static function user_before_delete($user) {
ORM::factory("user_chroot")->delete($user->id); ORM::factory('user_chroot', $user->id)->delete();
} }
/** /**
* Called when admin is adding a user * Called just before an item deletion.
*/
public static function item_before_delete($item) {
if( $item->is_album() ) {
ORM::factory('user_chroot')->where('album_id', '=', $item->id)->delete();
}
}
/**
* Called when building the 'Add user' form for an admin.
*/ */
static function user_add_form_admin($user, $form) { static function user_add_form_admin($user, $form) {
$form->add_user->dropdown("user_chroot") $form->add_user->dropdown('user_chroot')
->label(t("Root Album")) ->label(t("Root Album"))
->options(self::createGalleryArray()) ->options(self::albumsTreeArray())
->selected(0); ->selected(1);
} }
/** /**
* Called after a user has been added * Called just after a user has been added by an admin.
*/ */
static function user_add_form_admin_completed($user, $form) { public static function user_add_form_admin_completed($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find(); if( $form->add_user->user_chroot->value > 1 ) {
$user_chroot->id = $user->id; $user_chroot = ORM::factory('user_chroot');
$user_chroot->album_id = $form->add_user->user_chroot->value; $user_chroot->id = $user->id;
$user_chroot->save(); $user_chroot->album_id = $form->add_user->user_chroot->value;
} $user_chroot->save();
/**
* Called when admin is editing a user
*/
static function user_edit_form_admin($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find();
if ($user_chroot->loaded()) {
$selected = $user_chroot->album_id;
} else {
$selected = 0;
} }
$form->edit_user->dropdown("user_chroot") }
/**
* Called when building the 'Edit user' form for an admin.
*/
public static function user_edit_form_admin($user, $form) {
$user_chroot = ORM::factory('user_chroot', $user->id);
$selected = ( $user_chroot->loaded() )
? $user_chroot->album_id
: 1;
$form->edit_user->dropdown('user_chroot')
->label(t("Root Album")) ->label(t("Root Album"))
->options(self::createGalleryArray()) ->options(self::albumsTreeArray())
->selected($selected); ->selected($selected);
} }
/** /**
* Called after a user had been edited by the admin * Called just after a user has been edited by an admin.
*/ */
static function user_edit_form_admin_completed($user, $form) { public static function user_edit_form_admin_completed($user, $form) {
$user_chroot = ORM::factory("user_chroot")->where("id", "=", $user->id)->find(); if( $form->edit_user->user_chroot->value <= 1 ) {
if ($user_chroot->loaded()) { ORM::factory('user_chroot')->delete($user->id);
$user_chroot->album_id = $form->edit_user->user_chroot->value;
} else { } else {
$user_chroot->id = $user->id; $user_chroot = ORM::factory('user_chroot', $user->id);
if( !$user_chroot->loaded() ) {
$user_chroot = ORM::factory('user_chroot');
$user_chroot->id = $user->id;
}
$user_chroot->album_id = $form->edit_user->user_chroot->value; $user_chroot->album_id = $form->edit_user->user_chroot->value;
$user_chroot->save();
} }
$user_chroot->save();
}
/**
* Creates an array of galleries
*/
static function createGalleryArray() {
$array[0] = "none";
$root = ORM::factory("item", 1);
self::tree($root, "", $array);
return $array;
} }
/** /**
* recursive function to build array for drop down list * Generate an array representing the hierarchy of albums.
*/ */
static function tree($parent, $dashes, &$array) { private static function albumsTreeArray($level_marker = '    ') {
if ($parent->id == "1") { $tree = array();
$array[$parent->id] = ORM::factory("item", 1)->title; $albums = ORM::factory('item')
} else { ->where('type', '=', 'album')
$array[$parent->id] = "$dashes $parent->name"; ->order_by('left_ptr', 'ASC')
}
$albums = ORM::factory("item")
->where("parent_id", "=", $parent->id)
->where("type", "=", "album")
->order_by("title", "ASC")
->find_all(); ->find_all();
foreach ($albums as $album) {
self::tree($album, "-$dashes", $array); foreach($albums as $album) {
$tree[$album->id] = html::clean(
str_repeat($level_marker, $album->level - 1).' '.$album->title );
} }
return;
return $tree;
} }
} }

View File

@ -19,22 +19,25 @@
*/ */
class user_chroot_installer { class user_chroot_installer {
static function install() { /**
* Create the table user_chroot when installing the module.
*/
public static function install() {
$db = Database::instance(); $db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {user_chroots} ( $db->query('CREATE TABLE IF NOT EXISTS {user_chroots} (
`id` int(9) NOT NULL, `id` int(9) NOT NULL,
`album_id` int(9) default NULL, `album_id` int(9) default NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY(`id`)) UNIQUE KEY(`id`))
DEFAULT CHARSET=utf8;"); DEFAULT CHARSET=utf8;');
module::set_version("user_chroot", 1); module::set_version('user_chroot', 1);
} }
/** /**
* Drops the table of user chroot when the module is uninstalled. * Drops the table user_chroot when uninstalling the module.
*/ */
static function uninstall() { public static function uninstall() {
$db = Database::instance(); $db = Database::instance();
$db->query("DROP TABLE IF EXISTS {user_chroots};"); $db->query('DROP TABLE IF EXISTS {user_chroots};');
} }
} }

View File

@ -19,8 +19,10 @@
*/ */
class ORM_MPTT extends ORM_MPTT_Core { class ORM_MPTT extends ORM_MPTT_Core {
/**
* Copied from modules/gallery/libraries/ORM_MPTT.php, not sure of the reason...
*/
private $model_name = null; private $model_name = null;
function __construct($id=null) { function __construct($id=null) {
parent::__construct($id); parent::__construct($id);
$this->model_name = inflector::singular($this->table_name); $this->model_name = inflector::singular($this->table_name);
@ -31,13 +33,13 @@ class ORM_MPTT extends ORM_MPTT_Core {
* *
* @return ORM * @return ORM
*/ */
/*function parent() { function parent() {
if( user_chroot::album() && user_chroot::album()->id == $this->id ) { if( user_chroot::album() && user_chroot::album()->id == $this->id ) {
return null; return null;
} else { } else {
return parent::parent(); return parent::parent();
} }
}*/ }
/** /**
* Return all the parents of this node, in order from root to this node's immediate parent. * Return all the parents of this node, in order from root to this node's immediate parent.
@ -46,14 +48,14 @@ class ORM_MPTT extends ORM_MPTT_Core {
*/ */
function parents() { function parents() {
$select = $this $select = $this
->where("left_ptr", "<=", $this->left_ptr) ->where('left_ptr', '<=', $this->left_ptr)
->where("right_ptr", ">=", $this->right_ptr) ->where('right_ptr', '>=', $this->right_ptr)
->where("id", "<>", $this->id) ->where('id', '<>', $this->id)
->order_by("left_ptr", "ASC"); ->order_by('left_ptr', 'ASC');
if( user_chroot::album() ) { if( user_chroot::album() ) {
$select->where("left_ptr", ">=", user_chroot::album()->left_ptr); $select->where('left_ptr', '>=', user_chroot::album()->left_ptr);
$select->where("right_ptr", "<=", user_chroot::album()->right_ptr); $select->where('right_ptr', '<=', user_chroot::album()->right_ptr);
} }
return $select->find_all(); return $select->find_all();