Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: Added delete community
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Marion committed Mar 28, 2011
1 parent d3f1c47 commit 614a031
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 24 deletions.
38 changes: 34 additions & 4 deletions core/controllers/CommunityController.php
Expand Up @@ -40,28 +40,58 @@ function indexAction()
function viewAction()
{
$this->view->Date=$this->Component->Date;
//TODO: add policy check
$communityId=$this->_getParam("communityId");
if(!isset($communityId) || (!is_numeric($communityId) && strlen($communityId)!=32)) // This is tricky! and for Cassandra for now
{
throw new Zend_Exception("Community ID should be a number");
}
$communityDao=$this->Community->load($communityId);
if($communityDao===false)
if($communityDao===false||!$this->Community->policyCheck($communityDao, $this->userSession->Dao))
{
throw new Zend_Exception("This community doesn't exist.");
throw new Zend_Exception("This community doesn't exist or you don't have the permissions.");
}

$this->view->communityDao=$communityDao;
$this->view->information=array();
$this->view->feeds=$this->Feed->getFeedsByCommunity($this->userSession->Dao,$communityDao);

$group_member=$communityDao->getMemberGroup();
$this->view->members=$group_member->getUsers();

$this->view->folders=array();
$this->view->folders[]=$communityDao->getPublicFolder();
$this->view->folders[]=$communityDao->getPrivateFolder();
}//end index

$this->view->isModerator=$this->Community->policyCheck($communityDao, $this->userSession->Dao,MIDAS_POLICY_WRITE);
$this->view->isAdmin=$this->Community->policyCheck($communityDao, $this->userSession->Dao,MIDAS_POLICY_ADMIN);
$this->view->json['community']=$communityDao->_toArray();
$this->view->json['community']['message']['delete']=$this->t('Delete');
$this->view->json['community']['message']['deleteMessage']=$this->t('Do you really want to delete this community? It cannot be undo.');

}//end view

/** Delete a community*/
function deleteAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();

$communityId=$this->_getParam("communityId");
if(!isset($communityId) || (!is_numeric($communityId) && strlen($communityId)!=32)) // This is tricky! and for Cassandra for now
{
throw new Zend_Exception("Community ID should be a number");
}
$communityDao=$this->Community->load($communityId);
if($communityDao===false||!$this->Community->policyCheck($communityDao, $this->userSession->Dao,MIDAS_POLICY_ADMIN))
{
throw new Zend_Exception("This community doesn't exist or you don't have the permissions.");
}

$this->Community->delete($communityDao);

$this->_redirect('/');
}//end delete

/** create a community (ajax)*/
function createAction()
{
Expand Down
135 changes: 135 additions & 0 deletions core/models/base/CommunityModelBase.php
Expand Up @@ -32,5 +32,140 @@ public function __construct()
abstract function getPublicCommunities($limit=20);
abstract function getByName($name);


/** Delete a community */
function delete($communityDao)
{
if(!$communityDao instanceof CommunityDao)
{
throw new Zend_Exception("Error param.");
}
$this->ModelLoader = new MIDAS_ModelLoader();
$group_model=$this->ModelLoader->loadModel('Group');
$groups=$group_model->findByCommunity($communityDao);
foreach($groups as $group)
{
$group_model->delete($group);
}

$folder_model=$this->ModelLoader->loadModel('Folder');
$folder=$communityDao->getFolder();
$folder_model->delete($folder,true);

$feed_model=$this->ModelLoader->loadModel('Feed');
$feeds=$communityDao->getFeeds();
foreach($feeds as $feed)
{
$feed_model->delete($feed);
}
parent::delete($communityDao);
unset($communityDao->community_id);
$communityDao->saved=false;
}//end delete


/** check if the policy is valid
*
* @param FolderDao $folderDao
* @param UserDao $userDao
* @param type $policy
* @return boolean
*/
function policyCheck($communityDao,$userDao=null,$policy=0)
{
if(!$communityDao instanceof CommunityDao||!is_numeric($policy))
{
throw new Zend_Exception("Error param.");
}
if($userDao==null)
{
$userId= -1;
}
else if(!$userDao instanceof UserDao)
{
throw new Zend_Exception("Should be an user.");
}
else
{
$userId = $userDao->getUserId();
}

$privacy=$communityDao->getPrivacy();
switch ($policy)
{
case MIDAS_POLICY_READ:
if($privacy!=MIDAS_COMMUNITY_PRIVATE)
{
return true;
}
else if ($userId==-1)
{
return false;
}
else
{
$user_groups=$userDao->getGroups();
$member_group=$communityDao->getMemberGroup();
foreach($user_groups as $group)
{
if($group->getKey()==$member_group->getKey())
{
return true;
}
}
return false;
}
break;
case MIDAS_POLICY_WRITE:
if ($userId==-1)
{
return false;
}
else
{
$user_groups=$userDao->getGroups();
$moderator_group=$communityDao->getModeratorGroup();
$admin_group=$communityDao->getAdminGroup();
foreach($user_groups as $group)
{
if($group->getKey()==$moderator_group->getKey()|| $group->getKey()==$admin_group->getKey())
{
return true;
}
}
return false;
}
break;
case MIDAS_POLICY_ADMIN:
if ($userId==-1)
{
return false;
}
else
{
$user_groups=$userDao->getGroups();
$admin_group=$communityDao->getAdminGroup();
foreach($user_groups as $group)
{
if($group->getKey()==$admin_group->getKey())
{
return true;
}
}
return false;
}
break;
default:
return false;
break;
}

if(count($rowset)>0)
{
return true;
}
return false;
} //end policyCheck

} // end class CommunityModelBase
?>
3 changes: 2 additions & 1 deletion core/models/base/FolderModelBase.php
Expand Up @@ -32,6 +32,8 @@ abstract function getItemsFiltered($folder,$userDao=null,$policy=0);
abstract function getSizeFiltered($folders,$userDao=null,$policy=0);
abstract function getCommunity($folder);
abstract function getUser($folder);
abstract function addItem($folder,$item);
abstract function removeItem($folder,$item);

/** Create a folder */
function createFolder($name,$description,$parent)
Expand Down Expand Up @@ -61,6 +63,5 @@ function createFolder($name,$description,$parent)
$this->save($folder);
return $folder;
}

} // end class FolderModelBase
?>
20 changes: 20 additions & 0 deletions core/models/base/GroupModelBase.php
Expand Up @@ -18,7 +18,27 @@ public function __construct()

/** Add a user to a group */
abstract function addUser($group,$user);
abstract function removeUser($group,$user);
abstract function findByCommunity($communityDao);

/** Delete a group */
public function deleteGroup($group)
{
if(!$group instanceof GroupDao)
{
throw new Zend_Exception("Should be a group.");
}
$users=$group->getUsers();
foreach($users as $user)
{
$this->removeUser($group,$user);
}
parent::delete($group);
unset($group->group_id);
$group->saved=false;
}//end deleteGroup


/** create a group
* @return GroupDao*/
public function createGroup($communityDao,$name)
Expand Down
3 changes: 1 addition & 2 deletions core/models/pdo/CommunityModel.php
Expand Up @@ -6,8 +6,7 @@
* Pdo Model
*/
class CommunityModel extends CommunityModelBase
{

{
/** Get a community by name */
function getByName($name)
{
Expand Down
50 changes: 49 additions & 1 deletion core/models/pdo/FolderModel.php
Expand Up @@ -155,8 +155,9 @@ public function getSizeFiltered($folders,$userDao=null,$policy=0)
return $folders;
}


/** Custom delete function */
public function delete($folder)
function delete($folder,$recursive=false)
{
if(!$folder instanceof FolderDao)
{
Expand All @@ -171,6 +172,37 @@ public function delete($folder)
{
throw new Zend_Exception("Unable to find the key" );
}

$this->ModelLoader = new MIDAS_ModelLoader();
$items=$folder->getItems();
foreach($items as $item)
{
$this->removeItem($folder, $item);
}

if($recursive)
{
$children=$folder->getFolders();
foreach($children as $child)
{
$this->delete($child,true);
}
}

$policy_group_model=$this->ModelLoader->loadModel('Folderpolicygroup');
$policiesGroup=$folder->getFolderpolicygroup();
foreach($policiesGroup as $policy)
{
$policy_group_model->delete($policy);
}

$policy_user_model=$this->ModelLoader->loadModel('Folderpolicyuser');
$policiesUser=$folder->getFolderpolicyuser();
foreach($policiesUser as $policy)
{
$policy_user_model->delete($policy);
}

$leftIndice=$folder->getLeftIndice();
$this->database->getDB()->update('folder', array('left_indice'=> new Zend_Db_Expr('left_indice - 2')),
array('left_indice >= ?'=>$leftIndice));
Expand Down Expand Up @@ -495,6 +527,22 @@ function addItem($folder,$item)
}
$this->database->link('items',$folder,$item);
} // end function addItem

/** Remove an item from a folder
* @return void
*/
function removeItem($folder,$item)
{
if(!$folder instanceof FolderDao)
{
throw new Zend_Exception("Should be a folder.");
}
if(!$item instanceof ItemDao)
{
throw new Zend_Exception("Should be an item.");
}
$this->database->removeLink('items',$folder,$item);
} // end function addItem

/** Return an item by its name
* @return ItemDao*/
Expand Down
29 changes: 29 additions & 0 deletions core/models/pdo/GroupModel.php
Expand Up @@ -7,6 +7,19 @@
*/
class GroupModel extends GroupModelBase
{

/** Get a groups by Community */
function findByCommunity($communityDao)
{
$rowset = $this->database->fetchAll($this->database->select()->where('community_id = ?', $communityDao->getKey()));
$result=array();
foreach($rowset as $row)
{
$result[]=$this->initDao(ucfirst($this->_name),$row);
}
return $result;
} // end findByCommunity()

/** Add an user to a group
* @return void
* */
Expand All @@ -22,6 +35,22 @@ function addUser($group,$user)
}
$this->database->link('users',$group,$user);
} // end function addItem

/** Remove an user from a group
* @return void
* */
function removeUser($group,$user)
{
if(!$group instanceof GroupDao)
{
throw new Zend_Exception("Should be a group.");
}
if(!$user instanceof UserDao)
{
throw new Zend_Exception("Should be an user.");
}
$this->database->removeLink('users',$group,$user);
} // end function addItem

}// end class
?>

0 comments on commit 614a031

Please sign in to comment.