<?php

chdir(dirname(__FILE__));
header("Content-type: text/plain");
define("CONSOLE_MODE", true);
include "init.php";
Env::useHelper('format');
define('SCRIPT_MEMORY_LIMIT', 1024 * 1024 * 1024);
// 1 GB
@set_time_limit(0);
ini_set('memory_limit', SCRIPT_MEMORY_LIMIT / (1024 * 1024) + 50 . 'M');
$i = 0;
$objects_ids = Objects::instance()->findAll(array('columns' => array('id'), 'id' => true));
//,'conditions' => 'object_type_id = 6'
echo "\nObjects to process: " . count($objects_ids) . "\n-----------------------------------------------------------------";
foreach ($objects_ids as $object_id) {
    $object = Objects::findObject($object_id);
    $i++;
    if ($object instanceof ContentDataObject) {
        $members = $object->getMembers();
        DB::execute("DELETE FROM " . TABLE_PREFIX . "object_members WHERE object_id = " . $object->getId() . " AND is_optimization = 1;");
        ObjectMembers::addObjectToMembers($object->getId(), $members);
    } else {
        //
    }
    if ($i % 100 == 0) {
        echo "\n{$i} objects processed. Mem usage: " . format_filesize(memory_get_usage(true));
    }
}
Ejemplo n.º 2
0
function core_dimensions_after_save_member_permissions($member, &$ignored) {
	if (!$member instanceof Member || !($member->getId()>0)) return;
	$permission_group_ids = array();
	
	$cmp_rows = DB::executeAll("SELECT DISTINCT permission_group_id FROM ".TABLE_PREFIX."contact_member_permissions WHERE member_id = '".$member->getId()."' AND permission_group_id IN (SELECT id FROM ".TABLE_PREFIX."permission_groups WHERE type IN ('permission_groups','user_groups'))");
	foreach ($cmp_rows as $row) {
		$permission_group_ids[$row['permission_group_id']] = $row['permission_group_id'];
	}
	
	$contacts = array();
	// users
	if (count($permission_group_ids) > 0) {
		$contacts = Contacts::findAll(array('conditions' => 'user_type > 0 && permission_group_id IN ('.implode(',', $permission_group_ids).')'));
	}
	// contacts
	$contact_rows = DB::executeAll("SELECT DISTINCT om.object_id FROM ".TABLE_PREFIX."object_members om INNER JOIN ".TABLE_PREFIX."contacts c ON c.object_id=om.object_id 
		WHERE om.member_id='".$member->getId()."' AND c.user_type=0");
	$no_user_ids = array();
	if (is_array($contact_rows)) {
		foreach ($contact_rows as $row) {
			$no_user_ids[] = $row['object_id'];
		}
	}
	$more_contacts = Contacts::findAll(array('conditions' => 'object_id IN ('.implode(',', $no_user_ids).')'));
	
	$contacts = array_merge($contacts, $more_contacts);
	$contact_ids = array(0);
	
	$persons_dim = Dimensions::findByCode("feng_persons");

	core_dim_remove_contacts_member_associations($member);
	
	foreach ($contacts as $contact) {
		$contact_id = $contact->getId();
		$contact_member = Members::findOneByObjectId($contact_id, $persons_dim->getId());
		if ($contact_member instanceof Member) {
			core_dim_add_contact_member_associations($contact_member, $member);
			
			if ($contact instanceof Contact && $contact->isUser()) {
				$has_project_permissions = ContactMemberPermissions::instance()->count("permission_group_id = '".$contact->getPermissionGroupId()."' AND member_id = ".$member->getId()) > 0;
				if (!$has_project_permissions) {
					RoleObjectTypePermissions::createDefaultUserPermissions($contact, $member);
				}
			}
		}
		// add user content object to customer member
		ObjectMembers::addObjectToMembers($contact_id, array($member));
		$contact->addToSharingTable();
		$contact_ids[] = $contact_id;
	}
	
	// remove contacts whose members are no longer associated to the customer member
	$previous_users_in_member = Contacts::instance()->listing(array(
		'member_ids' => array($member->getId()),
		'ignore_context' => true,
		'extra_conditions' => ' AND e.user_type > 0 AND e.object_id NOT IN ('.implode(',', $contact_ids).')',
	))->objects;
	foreach ($previous_users_in_member as $prev_u) {
		ObjectMembers::removeObjectFromMembers($prev_u, logged_user(), array($member), array($member->getId()));
	}
	
	// refresh dimensions
	evt_add("reload dimension tree", array('dim_id' => $persons_dim->getId(), 'node' => null));
}
Ejemplo n.º 3
0
function create_user($user_data, $permissionsString) {
    
	// try to find contact by some properties 
	$contact_id = array_var($user_data, "contact_id") ;
	$contact =  Contacts::instance()->findById($contact_id) ; 
	
	if (!is_valid_email(array_var($user_data, 'email'))) {
		throw new Exception(lang("email value is required"));
	}

	if (!$contact instanceof Contact) {
		// Create a new user
		$contact = new Contact();
		$contact->setUsername(array_var($user_data, 'username'));
		$contact->setDisplayName(array_var($user_data, 'display_name'));
		$contact->setCompanyId(array_var($user_data, 'company_id'));
		$contact->setUserType(array_var($user_data, 'type'));
		$contact->setTimezone(array_var($user_data, 'timezone'));
		$contact->setFirstname($contact->getObjectName() != "" ? $contact->getObjectName() : $contact->getUsername());
		$contact->setObjectName();
	} else {
		// Create user from contact
		$contact->setUserType(array_var($user_data, 'type'));
		if (array_var($user_data, 'company_id')) {
			$contact->setCompanyId(array_var($user_data, 'company_id'));
		}	
		$contact->setUsername(array_var($user_data, 'username'));
		$contact->setTimezone(array_var($user_data, 'timezone'));
	}
	$contact->save();
	if (is_valid_email(array_var($user_data, 'email'))) {
		$contact->addEmail(array_var($user_data, 'email'), 'personal', true);
	}
	
	
	//permissions
	$permission_group = new PermissionGroup();
	$permission_group->setName('User '.$contact->getId().' Personal');
	$permission_group->setContactId($contact->getId());
	$permission_group->setIsContext(false);
	$permission_group->setType("permission_groups");
	$permission_group->save();
	$contact->setPermissionGroupId($permission_group->getId());
	
	$contact_pg = new ContactPermissionGroup();
	$contact_pg->setContactId($contact->getId());
	$contact_pg->setPermissionGroupId($permission_group->getId());
	$contact_pg->save();

	if ( can_manage_security(logged_user()) ) {
		
		$sp = new SystemPermission();
		$rol_permissions=SystemPermissions::getRolePermissions(array_var($user_data, 'type'));
		foreach($rol_permissions as $pr){
			$sp->setPermission($pr);
		}
		$sp->setPermissionGroupId($permission_group->getId());

		$sp->setCanManageSecurity(array_var($user_data, 'can_manage_security'));
		$sp->setCanManageConfiguration(array_var($user_data, 'can_manage_configuration'));
		$sp->setCanManageTemplates(array_var($user_data, 'can_manage_templates'));
		$sp->setCanManageTime(array_var($user_data, 'can_manage_time'));
		$sp->setCanAddMailAccounts(array_var($user_data, 'can_add_mail_accounts'));
		$sp->setCanManageDimensions(array_var($user_data, 'can_manage_dimensions'));
		$sp->setCanManageDimensionMembers(array_var($user_data, 'can_manage_dimension_members'));
		$sp->setCanManageTasks(array_var($user_data, 'can_manage_tasks'));
		$sp->setCanTasksAssignee(array_var($user_data, 'can_task_assignee'));
		$sp->setCanManageBilling(array_var($user_data, 'can_manage_billing'));
		$sp->setCanViewBilling(array_var($user_data, 'can_view_billing'));
		
		Hook::fire('add_user_permissions', $sp, $other_permissions);
		if (!is_null($other_permissions) && is_array($other_permissions)) {
			foreach ($other_permissions as $k => $v) {
				$sp->setColumnValue($k, array_var($user_data, $k));
			}
		}
		$sp->save();
		
		if ($contact->isAdminGroup()) {
			// allow all un all dimensions if new user is admin
			$dimensions = Dimensions::findAll();
			$permissions = array();
			foreach ($dimensions as $dimension) {
				if ($dimension->getDefinesPermissions()) {
					$cdp = ContactDimensionPermissions::findOne(array("conditions" => "`permission_group_id` = ".$contact->getPermissionGroupId()." AND `dimension_id` = ".$dimension->getId()));
					if (!$cdp instanceof ContactDimensionPermission) {
						$cdp = new ContactDimensionPermission();
						$cdp->setPermissionGroupId($contact->getPermissionGroupId());
						$cdp->setContactDimensionId($dimension->getId());
					}
					$cdp->setPermissionType('allow all');
					$cdp->save();
					
					// contact member permisssion entries
					$members = $dimension->getAllMembers();
					foreach ($members as $member) {
						
						$ots = DimensionObjectTypeContents::getContentObjectTypeIds($dimension->getId(), $member->getObjectTypeId());
						$ots[]=$member->getObjectId();
						foreach ($ots as $ot) {
							$cmp = ContactMemberPermissions::findOne(array("conditions" => "`permission_group_id` = ".$contact->getPermissionGroupId()." AND `member_id` = ".$member->getId()." AND `object_type_id` = $ot"));
							if (!$cmp instanceof ContactMemberPermission) {
								$cmp = new ContactMemberPermission();
								$cmp->setPermissionGroupId($contact->getPermissionGroupId());
								$cmp->setMemberId($member->getId());
								$cmp->setObjectTypeId($ot);
							}
							$cmp->setCanWrite(1);
							$cmp->setCanDelete(1);
							$cmp->save();
							
							// Add persmissions to sharing table
							$perm = new stdClass();
							$perm->m = $member->getId();
							$perm->r= 1;
							$perm->w= 1;
							$perm->d= 1;
							$perm->o= $ot;
							$permissions[] = $perm ;
						}
					}
				}
			}
			
			if(count($permissions)){
				$sharingTableController = new SharingTableController();
				$sharingTableController->afterPermissionChanged($contact->getPermissionGroupId(), $permissions);
			}
			
		}
		
	}
	if(!isset($_POST['sys_perm'])){
		$rol_permissions=SystemPermissions::getRolePermissions(array_var($user_data, 'type'));
		$_POST['sys_perm']=array();
		foreach($rol_permissions as $pr){
			$_POST['sys_perm'][$pr]=1;
		}
		
	}
	if(!isset($_POST['mod_perm'])){
		$tabs_permissions=TabPanelPermissions::getRoleModules(array_var($user_data, 'type'));
		$_POST['mod_perm']=array();
		foreach($tabs_permissions as $pr){
			$_POST['mod_perm'][$pr]=1;
		}
	}
        
    $password = '';
	if (array_var($user_data, 'password_generator') == 'specify') {
		$perform_password_validation = true;
		// Validate input
		$password = array_var($user_data, 'password');
		if (trim($password) == '') {
			throw new Error(lang('password value required'));
		} // if
		if ($password <> array_var($user_data, 'password_a')) {
			throw new Error(lang('passwords dont match'));
		} // if
	} else {
		$user_data['password_generator'] = 'link';
		$perform_password_validation = false;
	}

	$contact->setPassword($password);   
	$contact->save();

	$user_password = new ContactPassword();
	$user_password->setContactId($contact->getId());
	$user_password->setPasswordDate(DateTimeValueLib::now());
	$user_password->setPassword(cp_encrypt($password, $user_password->getPasswordDate()->getTimestamp()));
	$user_password->password_temp = $password;
	$user_password->perform_validation = $perform_password_validation;
	$user_password->save();
        
	if (array_var($user_data, 'autodetect_time_zone', 1) == 1) {
		set_user_config_option('autodetect_time_zone', 1, $contact->getId());
	}
	
	/* create contact for this user*/

	ApplicationLogs::createLog($contact, ApplicationLogs::ACTION_ADD);

	// Set role permissions for active members
	$active_context = active_context();
	$sel_members = array();
	foreach ($active_context as $selection) {
		if ($selection instanceof Member) {
			$sel_members[] = $selection;
			$has_project_permissions = ContactMemberPermissions::instance()->count("permission_group_id = '".$contact->getPermissionGroupId()."' AND member_id = ".$selection->getId()) > 0;
			if (!$has_project_permissions) {
				RoleObjectTypePermissions::createDefaultUserPermissions($contact, $selection);
			}
		}
	}
	save_permissions($contact->getPermissionGroupId(), $contact->isGuest());
	
	Hook::fire('after_user_add', $contact, $null);
	
	// add user content object to associated members
	if (count($sel_members) > 0) {
		ObjectMembers::addObjectToMembers($contact->getId(), $sel_members);
		$contact->addToSharingTable();
	}
	
	// Send notification
	try {
		if (array_var($user_data, 'send_email_notification') && $contact->getEmailAddress()) {
                    
			if (array_var($user_data, 'password_generator', 'link') == 'link') {
				// Generate link password
				$user = Contacts::getByEmail(array_var($user_data, 'email'));
				$token = sha1(gen_id() . (defined('SEED') ? SEED : ''));
				$timestamp = time() + 60*60*24;
				set_user_config_option('reset_password', $token . ";" . $timestamp, $user->getId());
				Notifier::newUserAccountLinkPassword($contact, $password, $token);

			} else {
				Notifier::newUserAccount($contact, $password);
			}
			
		}
	} catch(Exception $e) {
		Logger::log($e->getTraceAsString());
	} // try
	return $contact;
}
Ejemplo n.º 4
0
 function addToMembers($members_array)
 {
     ObjectMembers::addObjectToMembers($this->getId(), $members_array);
     if ($this instanceof ProjectFile) {
         $inline_images = ProjectFiles::findAll(array("conditions" => "mail_id = " . $this->getId()));
         foreach ($inline_images as $inline_img) {
             $inline_img->addToMembers($members_array);
             $inline_img->addToSharingTable();
         }
     }
 }
Ejemplo n.º 5
0
function associate_member_to_status_member($project_member, $old_project_status, $status_member_id, $status_dimension, $status_ot = null, $remove_prev_associations = true)
{
    if ($status_dimension instanceof Dimension && in_array($status_dimension->getId(), config_option('enabled_dimensions'))) {
        // asociate project objects to the new project_status member
        if ($old_project_status != $status_member_id) {
            $object_members = ObjectMembers::instance()->findAll(array('conditions' => "member_id = " . $project_member->getId() . " AND is_optimization=0"));
            // remove objects from old project_type member
            if ($old_project_status > 0) {
                foreach ($object_members as $om) {
                    $obj = Objects::findObject($om->getObjectId());
                    if ($obj instanceof ContentDataObject) {
                        ObjectMembers::removeObjectFromMembers($obj, logged_user(), null, array($old_project_status));
                    }
                }
            }
            // add objects to new project_type member
            if (is_numeric($status_member_id) && $status_member_id > 0) {
                $member_to_add = Members::findById($status_member_id);
                foreach ($object_members as $om) {
                    ObjectMembers::addObjectToMembers($om->getObjectId(), array($member_to_add));
                }
                if ($member_to_add instanceof Member && $member_to_add->getObjectId() > 0) {
                    $rel_obj = Objects::findObject($member_to_add->getObjectId());
                    if ($rel_obj instanceof ContentDataObject) {
                        ObjectMembers::addObjectToMembers($rel_obj->getId(), array($project_member));
                        $rel_obj->addToSharingTable();
                        $null = null;
                        Hook::fire("after_auto_classifying_associated_object_of_member", array('obj' => $rel_obj, 'mem' => $project_member), $null);
                    }
                }
            }
        }
        $member_dimension = $project_member->getDimension();
        $a = DimensionMemberAssociations::instance()->findOne(array('conditions' => array('dimension_id=? AND object_type_id=? AND associated_dimension_id=?' . ($status_ot instanceof ObjectType ? ' AND associated_object_type_id=' . $status_ot->getId() : ''), $member_dimension->getId(), $project_member->getObjectTypeId(), $status_dimension->getId())));
        // create relation between members and remove old relations
        if ($a instanceof DimensionMemberAssociation) {
            if (is_numeric($status_member_id) && $status_member_id > 0) {
                $mpm = MemberPropertyMembers::findOne(array('id' => true, 'conditions' => array('association_id = ? AND member_id = ? AND property_member_id = ?', $a->getId(), $project_member->getId(), $status_member_id)));
                if (is_null($mpm)) {
                    $sql = "INSERT INTO " . TABLE_PREFIX . "member_property_members (association_id, member_id, property_member_id, is_active, created_on, created_by_id)\r\n\t\t\t\t\t\tVALUES (" . $a->getId() . "," . $project_member->getId() . "," . $status_member_id . ", 1, NOW()," . logged_user()->getId() . ");";
                    DB::executeAll($sql);
                }
            }
            if ($remove_prev_associations) {
                MemberPropertyMembers::instance()->delete('association_id = ' . $a->getId() . ' AND member_id = ' . $project_member->getId() . " AND property_member_id <> '{$status_member_id}'");
            }
        }
        $a = DimensionMemberAssociations::instance()->findOne(array('conditions' => array('associated_dimension_id=? AND associated_object_type_id=? AND dimension_id=?' . ($status_ot instanceof ObjectType ? ' AND object_type_id=' . $status_ot->getId() : ''), $member_dimension->getId(), $project_member->getObjectTypeId(), $status_dimension->getId())));
        // create relation between members and remove old relations
        if ($a instanceof DimensionMemberAssociation) {
            if (is_numeric($status_member_id) && $status_member_id > 0) {
                $mpm = MemberPropertyMembers::findOne(array('id' => true, 'conditions' => array('association_id = ? AND member_id = ? AND property_member_id = ?', $a->getId(), $project_member->getId(), $status_member_id)));
                if (is_null($mpm)) {
                    $sql = "INSERT INTO " . TABLE_PREFIX . "member_property_members (association_id, member_id, property_member_id, is_active, created_on, created_by_id)\r\n\t\t\t\t\t\tVALUES (" . $a->getId() . "," . $status_member_id . "," . $project_member->getId() . ", 1, NOW()," . logged_user()->getId() . ");";
                    DB::executeAll($sql);
                }
            }
            if ($remove_prev_associations) {
                MemberPropertyMembers::instance()->delete('association_id = ' . $a->getId() . ' AND property_member_id = ' . $project_member->getId() . " AND member_id <> '{$status_member_id}'");
            }
        }
    }
}
 function addToMembers($members_array, $remove_old_comment_members = false)
 {
     ObjectMembers::addObjectToMembers($this->getId(), $members_array);
     /*if (Plugins::instance()->isActivePlugin('mail') && $this instanceof MailContent) {
     			$inline_images = ProjectFiles::findAll(array("conditions" => "mail_id = ".$this->getId()));
     			foreach ($inline_images as $inline_img) {
     				$inline_img->addToMembers($members_array);
     				$inline_img->addToSharingTable();
     			}
     		}*/
     if ($this->isCommentable()) {
         $comments = $this->getComments(true);
         foreach ($comments as $comment) {
             if ($remove_old_comment_members) {
                 ObjectMembers::instance()->delete("object_id = " . $comment->getId());
             }
             $comment->addToMembers($members_array);
             $comment->addToSharingTable();
         }
     }
 }