/**
  * Create an album for the newly created user and give him view and edit permissions.
  */
 static function user_created($user)
 {
     // Create a group with the same name, if necessary
     $group_name = "auto: {$user->name}";
     $group = identity::lookup_group_by_name($group_name);
     if (!$group) {
         $group = identity::create_group($group_name);
         identity::add_user_to_group($user, $group);
     }
     // Create an album for the user, if it doesn't exist
     $album = ORM::factory("item")->where("parent_id", "=", item::root()->id)->where("name", "=", $user->name)->find();
     if (!$album->loaded()) {
         $album->type = "album";
         $album->name = $user->name;
         $album->title = "{$user->name}'s album";
         $album->parent_id = item::root()->id;
         $album->sort_column = "weight";
         $album->sort_order = "asc";
         $album->save();
         access::allow($group, "view", item::root());
         access::allow($group, "view_full", $album);
         access::allow($group, "edit", $album);
         access::allow($group, "add", $album);
     }
 }
Exemple #2
0
 /**
  * Import a single group.
  */
 static function import_group(&$queue)
 {
     $messages = array();
     $g2_group_id = array_shift($queue);
     if (self::map($g2_group_id)) {
         return;
     }
     try {
         $g2_group = g2(GalleryCoreApi::loadEntitiesById($g2_group_id));
     } catch (Exception $e) {
         throw new G2_Import_Exception(t("Failed to import Gallery 2 group with id: %id,", array("id" => $g2_group_id)), $e);
     }
     switch ($g2_group->getGroupType()) {
         case GROUP_NORMAL:
             try {
                 $group = identity::create_group($g2_group->getGroupName());
                 $messages[] = t("Group '%name' was imported", array("name" => $g2_group->getGroupname()));
             } catch (Exception $e) {
                 // Did it fail because of a duplicate group name?
                 $group = identity::lookup_group_by_name($g2_group->getGroupname());
                 if ($group) {
                     $messages[] = t("Group '%name' was mapped to the existing group group of the same name.", array("name" => $g2_group->getGroupname()));
                 } else {
                     throw new G2_Import_Exception(t("Failed to import group '%name'", array("name" => $g2_group->getGroupname())), $e);
                 }
             }
             break;
         case GROUP_ALL_USERS:
             $group = identity::registered_users();
             $messages[] = t("Group 'Registered' was converted to '%name'", array("name" => $group->name));
             break;
         case GROUP_SITE_ADMINS:
             $messages[] = t("Group 'Admin' does not exist in Gallery 3, skipping");
             break;
             // This is not a group in G3
         // This is not a group in G3
         case GROUP_EVERYBODY:
             $group = identity::everybody();
             $messages[] = t("Group 'Everybody' was converted to '%name'", array("name" => $group->name));
             break;
     }
     if (isset($group)) {
         self::set_map($g2_group->getId(), $group->id, "group");
     }
     return $messages;
 }
 public function i_can_edit_test()
 {
     // Create a new user that belongs to no groups
     $user = identity::create_user("access_test", "Access Test", "*****", "*****@*****.**");
     foreach ($user->groups() as $group) {
         $user->remove($group);
     }
     $user->save();
     identity::set_active_user($user);
     // This user can't edit anything
     $root = item::root();
     $this->assert_false(access::can("edit", $root));
     // Now add them to a group that has edit permission
     $group = identity::create_group("access_test");
     $group->add($user);
     $group->save();
     access::allow($group, "edit", $root);
     $user = identity::lookup_user($user->id);
     // reload() does not flush related columns
     identity::set_active_user($user);
     // And verify that the user can edit.
     $this->assert_true(access::can("edit", $root));
 }
Exemple #4
0
 /**
  * Import a single group.
  */
 static function import_group(&$queue)
 {
     $g2_group_id = array_shift($queue);
     if (self::map($g2_group_id)) {
         return;
     }
     try {
         $g2_group = g2(GalleryCoreApi::loadEntitiesById($g2_group_id));
     } catch (Exception $e) {
         return t("Failed to import Gallery 2 group with id: %id\n%exception", array("id" => $g2_group_id, "exception" => $e->__toString()));
     }
     switch ($g2_group->getGroupType()) {
         case GROUP_NORMAL:
             try {
                 $group = identity::create_group($g2_group->getGroupName());
             } catch (Exception $e) {
                 // @todo For now we assume this is a "duplicate group" exception
                 $group = identity::lookup_user_by_name($g2_group->getGroupname());
             }
             $message = t("Group '%name' was imported", array("name" => $g2_group->getGroupname()));
             break;
         case GROUP_ALL_USERS:
             $group = identity::registered_users();
             $message = t("Group 'Registered' was converted to '%name'", array("name" => $group->name));
             break;
         case GROUP_SITE_ADMINS:
             $message = t("Group 'Admin' does not exist in Gallery 3, skipping");
             break;
             // This is not a group in G3
         // This is not a group in G3
         case GROUP_EVERYBODY:
             $group = identity::everybody();
             $message = t("Group 'Everybody' was converted to '%name'", array("name" => $group->name));
             break;
     }
     if (isset($group)) {
         self::set_map($g2_group->getId(), $group->id);
     }
     return $message;
 }
Exemple #5
0
 static function random_group()
 {
     return identity::create_group((string) rand());
 }
 static function album_add_form_completed($album, $form)
 {
     if ($form->privacy->private->checked) {
         $username = $form->privacy->username->value;
         $password = $form->privacy->password->value;
         // TODO validation
         // create a group based on username
         $group = identity::create_group($username);
         // create a user based on username
         $user = identity::create_user($username, $username, $password, $username . "@unknown.com");
         identity::add_user_to_group($user, $group);
         // create user home
         $home = ORM::factory("user_home")->where("id", "=", $user->id)->find();
         $home->id = $user->id;
         $home->home = $album->id;
         $home->save();
         // reload album
         $album->reload();
         // set permissions
         // deny all groups.
         $groups = ORM::factory("group")->find_all();
         foreach ($groups as $group2) {
             if ($group->id != $group2->id) {
                 access::deny($group2, "view", $album);
                 access::deny($group2, "view_full", $album);
             }
         }
         // deny all other albums
         $albums = ORM::factory("item")->where("type", "=", "album")->find_all();
         foreach ($albums as $albumt) {
             access::deny($group, "view", $albumt);
         }
         // allow access to newly created group
         access::allow($group, "view_full", $album);
         $parents = $album->parents();
         foreach ($parents as $parent) {
             access::allow($group, "view", $parent);
         }
         access::allow($group, "view", $album);
     }
 }
Exemple #7
0
 static function random_group()
 {
     return identity::create_group(random::string(6));
 }
Exemple #8
0
 static function random_group()
 {
     return identity::create_group(test::random_string(6));
 }