summaryrefslogblamecommitdiffstats
path: root/application/modules/user/controllers/PersonController.php
blob: 168add9c11191815792876ae993b0abc6668b88d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
     









                                                                           
 
                                                          
 
 
                                 
                                       
                                           
                                      
                                      
                                 
                                             
                                           


                              

                                                                                   
                                                                                        
                                                                        





                                                                                                                                      


                                                                                               
                                                                                                                               
                                
                                                                                     
                                                                         

                                                                         
                                

                                                       


                                                                                                  
                                                                                                                                                   

                                                                                         

                                                                                                      
                                                                 




                                                                                                                   







                                                                    
 

                                     


                                                  
                                                                            

                                                                      

                                                                           
                                                                          
                                                                       

                                                                        
















                                                                                           














                                                                                             
                                            




                                                                                                                                
                                                                    




                                                                                


                                                   
                                                    

                                                                                                                     
                                                    






                                                                                                    
                                                         


                                                                                  

                                                   
                                            




                                                                                                                                
                                                                    
                                                                                

                                                                                               
                                                                     
                                                                    
                                                                       
                                                                                      
         
 

                                    


                                                  
                                                                       
                                                                                      

                                                                                                               
                         



                                                                                                                   



                                                            




                                                                     
                                                                                                                
                                                                    
                                                                                           
                                                                         
                                                                                            
                                                                                               
                                                                                                                                    
                                                                                                                               
                                         



                                                                                   


                                                                                                                                      

                                                       
                                                                                 

                                                                          
                                                                                           
                                                                         
                                                                                                  
                                                                                                     
                                                                                                                                                

                                                                                                                                                 



                                                                                         


                                                                                                                                      

                                                       
                                                                                      
                                 




                                                  
 

                                       


                                                  

                                                            
                                                                                                                     












                                                                                 
                                                                                                         














                                                                             
                                                                                   
                                       










                                                                                                            














                                                                                                       
                                 
                                                                                   







                                                        


                                                  












                                                                                    
                                                                           


                               






                                                                          








                                                                                                                











                                                                                                            
                                                                                                                
                                                                       


                                                                                                            
                                                                                        
                                                                                          


                                                                                                                                      






                                                                                                                                      

                                                         



                                                                                                                                                                                 
                                                 
                                         

                                                                                                                                           
                                 

                                                                                



                         







                                                                         

                                      
                                                 

                                                  
                                                                                 

         












                                                                                                                   



                                                                                                                             



                                                      












                                                                                                                       

                                                           
                                                    




                                                                                                                                                             
                                                                            
                                                                                        


                                                                             
                                                                               





                                                                                              


                                             

                                                  
                 













                                                                                 








                                                                       
                                               












                                                                                            
 








                                                                       
                                               












                                                                                            

 
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class user_PersonController extends Zend_Controller_Action
{

	protected $person = null;
	protected $personmapper = null;
	protected $membershipMapper = null;
	protected $memberships = null;
	protected $groupMapper = null;
	protected $groups = null;
	protected $groupRequestMapper = null;
	protected $userIDsNamespace = null;

	public function init()
	{
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$this->personmapper = new Application_Model_PersonMapper();
			$this->userIDsNamespace = Zend_Session::namespaceGet('userIDs');
			if(isset($this->userIDsNamespace['personID'])) {
				$this->person = $this->personmapper->find($this->userIDsNamespace['personID']);
			} else {
				$result = $this->personmapper->findBy(array('email' => Zend_Auth::getInstance()->getIdentity()),true);
				$this->person = new Application_Model_Person($result[0]);
				$this->person->setID($result[0]['personID']);
			}
			$this->groupMapper = new Application_Model_GroupMapper();
			$this->groupRequestMapper = new Application_Model_GroupRequestMapper();
			$this->membershipMapper = new Application_Model_MembershipMapper();
			$this->memberships = $this->membershipMapper->findBy(array("personID" => $this->person->getID()),true);
				
			$rightrolesMapper = new Application_Model_RightRolesMapper();
			$rightroles = new Application_Model_RightRoles();
			$role = new Application_Model_Role();
			$roleMapper = new Application_Model_RoleMapper();
				
			$this->view->apikeys = array();

			if(isset($this->memberships)) {
				foreach($this->memberships as $membership) {
					$group = $this->groupMapper->find($membership['groupID']);
					list($rightroles) = $rightrolesMapper->findBy(array('rightID' => '55', 'roleID' => $membership['roleID']));
					$role = $roleMapper->find($membership['roleID']);
					if($rightroles != null)
					$this->view->apikeys[$group->getID()] = $membership['apikey'];
						
					$this->groups[] = array (
                        					'groupID'  => $group->getID(),	
                        					'title' => $group->getTitle(),
                        					'description' => $group->getDescription(),
                        					'membershipID' => $membership['membershipID'],
											'role' => $role->getTitle()
					);
				}
			}
		} else {
			$this->_helper->redirector('login', 'auth');
			return;
		}
	}

	public function indexAction()
	{
		if(!Pbs_Acl::checkRight('pso')) {
			$this->_redirect('/user');
		}
		$this->view->showRight = true;//Pbs_Acl::checkRight('psod');
		$this->view->editRight = Pbs_Acl::checkRight('peod');
		$this->view->deleteRight = Pbs_Acl::checkRight('pdo');
		$this->view->showOtherRight = Pbs_Acl::checkRight('psood');
		$this->view->editOtherRight = Pbs_Acl::checkRight('peoa');
		$this->view->deleteOtherRight = Pbs_Acl::checkRight('pd');
		$this->view->suspendRight = Pbs_Acl::checkRight('psa');
		$this->view->userIDsNamespace = $this->userIDsNamespace;

		$result = $this->_request->getParam('deleteresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('delete',$result);
		}
		$result = $this->_request->getParam('modifyresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('modify',$result);
		}
		$result = $this->_request->getParam('addresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('create',$result);
		}


		$this->view->personList = $this->personmapper->fetchAll();

		// Search
		$search = $this->_request->getParam('search');
		$mySearch = new Pbs_Search();
		$mySearch->setSearchTerm($search);
		$mySearch->setModule('person');
		if($search != ''){
			$this->view->search = $mySearch->getSearchTerm();
			$this->view->personList = $mySearch->search($this->view->personList);
		}
		$this->view->searchform = $mySearch->searchForm();

		// Pagination
		$pagination = new Pbs_Pagination();
		$pagination->setPerPage(10);
		$pagination->setElement($this->view->personList);
		$pagination->setRequestPage($this->_request->getParam('page'));
		$pagination->setPageUrl('/user/person/index/'.((isset($this->view->search))?'/search/'.$this->view->search:''));

		$this->view->personList = $pagination->getElements();
		$this->view->pagination = $pagination->pagination();
		$this->view->page 		= $pagination->getRequestPage();
	}

	public function owndetailsAction()
	{
		#if(!Pbs_Acl::checkRight('psod')) {
		#	$this->_redirect('/user');
		#}
		$this->view->person = $this->person;
		$groupRequests = $this->groupRequestMapper->findBy(array('personID' => $this->person->getID()),true);
		if(isset($groupRequests)) {
			$groupRequestList = array();
			foreach($groupRequests as $groupRequest) {
				$group = $this->groupMapper->find($groupRequest['groupID']);
				$groupRequestList[] = array(
        					'grouprequestID' => $groupRequest['grouprequestID'],
        					'group' => $group
				);
			}
			if(count($groupRequestList)> 0) {
				$this->view->groupRequestList = $groupRequestList;
			}
		}
		// Pagination
		$pagination = new Pbs_Pagination();
		$pagination->setPerPage(10);
		$pagination->setElement($this->groups);
		$pagination->setRequestPage($this->_request->getParam('page'));
		$pagination->setPageUrl('/user/person/index' .((isset($this->view->search))?'/search/'.$this->view->search:''));

		$this->view->groups = $pagination->getElements();
		$this->view->pagination = $pagination->pagination();
		$this->view->page 		= $pagination->getRequestPage();
		// This should be activated in case the person has no membership and no rights.
		$this->view->groupRequestRight = true;
		$this->view->editRight = Pbs_Acl::checkRight('peod');
		$this->view->leaveRight = Pbs_Acl::checkRight('gl');
		$this->view->suspendRight = Pbs_Acl::checkRight('psa');
		$this->view->userIDsNamespace = Zend_Session::namespaceGet('userIDs');
	}

	public function editAction()
	{
		if(!Pbs_Acl::checkRight('peod')) {
			$this->_redirect('/user');
		}
		$requestedUser = $this->_request->getParam('personID');
		if($requestedUser != "" && $this->person->getID() != $requestedUser) {
			if(!is_numeric($requestedUser)){
				$this->_redirect('/user/person/index/page/'.$this->page.'/modifyresult/error');
			}
			if(!Pbs_Acl::checkRight('peoa')) {
				$this->_redirect('/user/person/index/page/'.$this->page.'/modifyresult/forbidden');
			}			
			$person = $this->personmapper->find($requestedUser);
			$this->view->person = $person;
		} else {
			$this->view->person = $this->person;
		}
		if (!isset($_POST["save"])){
			$editForm = new user_Form_PersonEdit();
		} else {
			$editForm = new user_Form_PersonEdit($_POST);
			if ($editForm->isValid($_POST)) {
				if($this->person->getID() == $requestedUser) {					
					$person->setOptions($_POST);
					$this->person->setID($this->view->person->getID());
					if($_POST['newpassword'] != '') {
						$person->setPassword($_POST['newpassword']);
						$person->setPasswordSalt(MD5(microtime(true)));
						$person->setLoginPassword(crypt($person->getPassword(), '$6$'.randomString(8).'$'));
						$person->setPassword(MD5($person->getPassword() . $person->getPasswordSalt()));
					}
					try {
						$this->personmapper->save($person);
					} catch(Zend_Exception $e)
					{
						$pbsNotifier = new Pbs_Notifier();
						$this->view->notification = $pbsNotifier->notify('Email already registered', 'error');
						$this->view->registerForm = $registerForm;
						return;
					}
					$this->_helper->redirector('', 'person');
				} else {				
					$this->person->setOptions($_POST);
					$this->person->setID($this->view->person->getID());
					if($_POST['newpassword'] != '') {
						$this->person->setPassword($_POST['newpassword']);
						$this->person->setPasswordSalt(MD5(microtime(true)));
						$this->person->setLoginPassword(crypt($this->person->getPassword(), '$6$'.randomString(8).'$'));
						$this->person->setPassword(MD5($this->person->getPassword() . $this->person->getPasswordSalt()));
					}
					try {
						$this->personmapper->save($this->person);
					} catch(Zend_Exception $e)
					{
						$pbsNotifier = new Pbs_Notifier();
						$this->view->notification = $pbsNotifier->notify('Email already registered', 'error');
						$this->view->registerForm = $registerForm;
						return;
					}
					$this->_helper->redirector('index', 'person');
				}
				return;
			}
		}
		$this->view->editForm = $editForm;
	}

	public function requestAction()
	{
		#if(!Pbs_Acl::checkRight('grm')) {
		#	$this->_redirect('/user');
		#}
		$this->view->person = $this->person;
		$allgroups = $this->groupMapper->fetchAll();
		$groupRequests = $this->groupRequestMapper->findBy(array('personID' => $this->person->getID()),true);
		$count = 0;
		foreach($allgroups as $group) {
			foreach($groupRequests as $groupRequest) {
				if($groupRequest['groupID'] == $group->getID()) {
					$groupsFound[$count] = true;
				} else {
					$groupsFound[$count] = false;
				}
			}
			foreach($this->memberships as $membership) {
				if($membership['groupID'] == $group->getID()) {
					$groupsFound[$count] = true;
				} else {
					if(isset($groupsFound[$count]) && $groupsFound[$count] != true) {
						$groupsFound[$count] = false;
					}
				}
			}
			$count++;
		}
		if(count($groupsFound) > 0) {
			foreach($groupsFound as $k => $v) {
				if($v == true) {
					unset($allgroups[$k]);
				}
			}
		}
		if (!isset($_POST["request"])){
			if(count($allgroups) <= 0) {
				$this->_helper->redirector('owndetails', 'person');
				return;
			}
			$requestForm = new user_Form_GroupRequest(array('grouplist' => $allgroups));
		} else {
			$requestForm = new user_Form_GroupRequest(array('grouplist' => $allgroups), $_POST);
			if ($requestForm->isValid($_POST)) {
				$groupRequestMapper = new Application_Model_GroupRequestMapper();
				$groupRequest = new Application_Model_GroupRequest();
				$groupRequest->setGroupID($_POST['groupID']);
				$date = new DateTime();
				$groupRequest->setTime($date->getTimestamp());
				$groupRequest->setPersonID($this->person->getID());
				foreach($groupRequests as $groupRequestDB) {
					if($groupRequestDB['groupID'] == $groupRequest->getGroupID()) {
						$requestFound = true;
						break;
					}
				}
				if(!$requestFound) {
					try {
						$groupRequestMapper->save($groupRequest);
					} catch(Zend_Exception $e)
					{
						echo "Caught exception: " . get_class($e) . "<br/>";
						echo "Message: " . $e->getMessage() . "<br/>";
						return;
					}
				}
				$this->_helper->redirector('owndetails', 'person');
				return;
			}
		}
		$this->view->requestForm = $requestForm;
	}

	public function leaveAction()
	{
		if(!Pbs_Acl::checkRight('gl')) {
			$this->_redirect('/user');
		}
		$this->_helper-> viewRenderer-> setNoRender();
		$this->view->person = $this->person;
		$membershipID = $this->_request->getParam('membershipID');
		if(isset($membershipID)) {
			$membership = $this->membershipMapper->find($membershipID);
			try {
				$this->membershipMapper->delete($membership);
			} catch(Zend_Exception $e)
			{
				echo "Caught exception: " . get_class($e) . "<br/>";
				echo "Message: " . $e->getMessage() . "<br/>";
				return;
			}
			$this->_helper->redirector('owndetails', 'person');
			return;
		}
	}

	public function selectmembershipAction()
	{
		$userIDsNamespace = Zend_Session::namespaceGet('userIDs');
		if(isset($userIDsNamespace['membershipID'])) {
			$this->_redirect('/user/');
		} else {
			if(count($this->memberships) == 1) {	
				$myMembership = $this->memberships[0];
				$roleSession = new Zend_Session_Namespace('userIDs');				
				$roleSession->membershipID = $myMembership['membershipID'];
				$roleSession->personID = $myMembership['personID'];
				$roleSession->groupID = $myMembership['groupID'];
				$roleSession->roleID = $myMembership['roleID'];		
				$this->_redirect('/user/');
			} elseif(count($this->memberships) > 1) {
				if(isset($_POST['selectmembership'])) {
					$roleSession = new Zend_Session_Namespace('userIDs');
					$roleSession->membershipID = $_POST['membershipID'];
					$roleSession->personID = $this->person->getID();
					$membership = $this->membershipMapper->find($_POST['membershipID']);
					$roleSession->groupID = $membership->getGroupID();
					$roleSession->roleID = $membership->getRoleID();
					$this->_redirect('/user/');
					return;
				} else {
					$groupMapper = new Application_Model_GroupMapper();
					$roleMapper = new Application_Model_RoleMapper();
					if(isset($this->memberships)) {						
						$suspendlist = array();
						foreach($this->memberships as $membership) {
							$group = $groupMapper->find($membership['groupID']);
							$role = $roleMapper->find($membership['roleID']);
							if($membership['suspend'] == 0){
								$membershipList[] = array(
                                							'membershipID' => $membership['membershipID'],
                                							'group' => $group->getTitle(),
                                							'role' => $role->getTitle()
								);
							}
							else{
								$suspendlist[] = array(
                                							'membershipID' => $membership['membershipID'],
                                							'group' => $group->getTitle(),
                                							'role' => $role->getTitle());
							}

						}
						if(count($suspendlist) >=1){
							$pbsNotifier = new Pbs_Notifier();
							$this->view->notification = $pbsNotifier->notify("Actually ".count($suspendlist)." Memberships are suspended", 'error' );
						}
					}
					$membershipSelectForm = new user_Form_MembershipSelect(array('membershiplist' => $membershipList));
					$this->view->membershipSelectForm = $membershipSelectForm;
				}
			} else {
				$this->_helper->redirector('request', 'person');
			}
		}
	}

	public function changemembershipAction()
	{
		$this->_helper-> viewRenderer-> setNoRender();
		Zend_Session::namespaceUnset('userIDs');
		$this->_helper->redirector('selectmembership', 'person');
		return;
	}

	public function searchAction()
	{
		if(!Pbs_Acl::checkRight('pso')) {
			$this->_redirect('/user');
		}
		$this->_redirect('/user/person/index/search/'.($_GET['search']));
	}

	public function showAction()
	{
		if(!Pbs_Acl::checkRight('psood')) {
			$this->_redirect('/user');
		}
		$personID = $this->_request->getParam('personID');
		if($personID) {
			$person = $this->personmapper->find($personID);
			$memberships = $this->membershipMapper->findBy(array("personID" => $person->getID()),true);
			if(isset($memberships)) {
				foreach($memberships as $membership) {
					$group = $this->groupMapper->find($membership['groupID']);
					$groups[] = array (
                                        					'groupID'  => $group->getID(),	
                                        					'title' => $group->getTitle(),
                                        					'description' => $group->getDescription(),
                                        					'membershipID' => $membership['membershipID']
					);
				}
			}
			$this->view->person = $person;
			$groupRequests = $this->groupRequestMapper->findBy(array('personID' => $person->getID()),true);
			if(isset($groupRequests)) {
				foreach($groupRequests as $groupRequest) {
					$group = $this->groupMapper->find($groupRequest['groupID']);
					$groupRequestList[] = array(
        					'grouprequestID' => $groupRequest['grouprequestID'],
        					'group' => $group
					);
				}
				if(is_array($groupRequestList)) {
					$this->view->groupRequestList = $groupRequestList;
				}
			}
			// Pagination
			$pagination = new Pbs_Pagination();
			$pagination->setPerPage(10);
			$pagination->setElement($groups);
			$pagination->setRequestPage($this->_request->getParam('page'));
			$pagination->setPageUrl('/user/person/show/personID/' . $personID .((isset($this->view->search))?'/search/'.$this->view->search:''));

			$this->view->groups = $pagination->getElements();
			$this->view->pagination = $pagination->pagination();
			$this->view->page 		= $pagination->getRequestPage();
			$this->view->personID = $personID;
			$this->view->editRight = Pbs_Acl::checkRight('peoa');
			$this->view->deleteRight = Pbs_Acl::checkRight('pd');
			$this->view->suspendRight = Pbs_Acl::checkRight('psa');
			$this->view->userIDsNamespace = Zend_Session::namespaceGet('userIDs');
		} else {
			$this->_helper->redirector('', 'person');
			return;
		}
	}

	public function deleterequestAction()
	{
		if(!Pbs_Acl::checkRight('gdm')) {
			$this->_redirect('/user');
		}
		$grouprequestID = $this->_request->getParam('grouprequestID');
		$grouprequest = $this->groupRequestMapper->find($grouprequestID);
		try {
			$this->groupRequestMapper->delete($grouprequest);
		} catch(Zend_Exception $e)
		{
			echo "Caught exception: " . get_class($e) . "<br/>";
			echo "Message: " . $e->getMessage() . "<br/>";
			return;
		}
		$this->_helper->redirector('owndetails', 'person');
		return;
	}

	public function suspendAction()
	{
		if(!Pbs_Acl::checkRight('psa')) {
			$this->_redirect('/user');
		}
		$this->_helper->viewRenderer->setNoRender();
		$personID = $this->_request->getParam('personID');
		if(isset($personID)) {
			$person = $this->personmapper->find($personID);
			$person->setSuspend(1);
			if(isset($person)) {
				try {
					$this->personmapper->save($person);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				$this->_redirect("/user/person/");
			}
		}
	}

	public function resumeAction()
	{
		if(!Pbs_Acl::checkRight('psa')) {
			$this->_redirect('/user');
		}
		$this->_helper->viewRenderer->setNoRender();
		$personID = $this->_request->getParam('personID');
		if(isset($personID)) {
			$person = $this->personmapper->find($personID);
			$person->setSuspend(0);
			if(isset($person)) {
				try {
					$this->personmapper->save($person);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				$this->_redirect("/user/person/");
			}
		}
	}
}