Explorar o código

New lifecycle action SetCurrentPerson. Also improved the existing lifecycle action SetCurrentUser to prevent from calling it on an external key that is not pointing to users (!= contact), and if the target attribute is a string, then store the friendlyname there.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3616 a333f486-631f-4898-b8df-5754b55c2be0
romainq %!s(int64=10) %!d(string=hai) anos
pai
achega
c09b913411

+ 50 - 1
core/dbobject.class.php

@@ -2224,7 +2224,56 @@ abstract class DBObject implements iDisplay
 	 */	 	
 	public function SetCurrentUser($sAttCode)
 	{
-		$this->Set($sAttCode, UserRights::GetUserId());
+		$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
+		if ($oAttDef instanceof AttributeString)
+		{
+			// Note: the user friendly name is the contact friendly name if a contact is attached to the logged in user
+			$this->Set($sAttCode, UserRights::GetUserFriendlyName());
+		}
+		else
+		{
+			if ($oAttDef->IsExternalKey())
+			{
+				if ($oAttDef->GetTargetClass() != 'User')
+				{
+					throw new Exception("SetCurrentUser: the attribute $sAttCode must be an external key to 'User', found '".$oAttDef->GetTargetClass()."'");
+				}
+			}
+			$this->Set($sAttCode, UserRights::GetUserId());
+		}
+		return true;
+	}
+
+	/**
+	 * Lifecycle action: Set the current logged in CONTACT for the given attribute
+	 */	 	
+	public function SetCurrentPerson($sAttCode)
+	{
+		$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
+		if ($oAttDef instanceof AttributeString)
+		{
+			$iPerson = UserRights::GetContactId();
+			if ($iPerson == 0)
+			{
+				$this->Set($sAttCode, '');
+			}
+			else
+			{
+				$oPerson = MetaModel::GetObject('Person', $iPerson);
+				$this->Set($sAttCode, $oPerson->Get('friendlyname'));
+			}
+		}
+		else
+		{
+			if ($oAttDef->IsExternalKey())
+			{
+				if (!MetaModel::IsParentClass($oAttDef->GetTargetClass(), 'Person'))
+				{
+					throw new Exception("SetCurrentContact: the attribute $sAttCode must be an external key to 'Person' or any other class above 'Person', found '".$oAttDef->GetTargetClass()."'");
+				}
+			}
+			$this->Set($sAttCode, UserRights::GetContactId());
+		}
 		return true;
 	}
 

+ 16 - 0
datamodels/2.x/itop-tickets/datamodel.itop-tickets.xml

@@ -818,6 +818,22 @@
               </argument>
             </arguments>
           </method>
+          <method id="SetCurrentPerson">
+            <arguments>
+              <argument id="1">
+                <type>attcode</type>
+                <mandatory>true</mandatory>
+                <type_restrictions>
+                  <operation>allow</operation>
+                  <types>
+                    <type id="AttributeExternalKey"/>
+                    <type id="AttributeInteger"/>
+                    <type id="AttributeString"/>
+                  </types>
+                </type_restrictions>
+              </argument>
+            </arguments>
+          </method>
           <method id="SetElapsedTime">
             <arguments>
               <argument id="1">

+ 5 - 1
datamodels/2.x/itop-tickets/en.dict.itop-tickets.php

@@ -203,7 +203,11 @@ Dict::Add('EN US', 'English', 'English', array(
 	'Class:cmdbAbstractObject/Method:SetCurrentUser' => 'SetCurrentUser',
 	'Class:cmdbAbstractObject/Method:SetCurrentUser+' => 'Set a field with the currently logged in user',
 	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1' => 'Target Field',
-	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1+' => 'The field to set, in the current object',
+	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1+' => 'The field to set, in the current object. If the field is a string then the friendly name will be used, otherwise the identifier will be used. That friendly name is the name of the person if any is attached to the user, otherwise it is the login.',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson' => 'SetCurrentPerson',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson+' => 'Set a field with the currently logged in person (the "person" attached to the logged in "user").',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson/Param:1' => 'Target Field',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson/Param:1+' => 'The field to set, in the current object. If the field is a string then the friendly name will be used, otherwise the identifier will be used.',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime' => 'SetElapsedTime',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime+' => 'Set a field with the time (seconds) elapsed since a date given by another field',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime/Param:1' => 'Target Field',

+ 5 - 1
datamodels/2.x/itop-tickets/fr.dict.itop-tickets.php

@@ -188,7 +188,11 @@ Dict::Add('FR FR', 'French', 'Français', array(
 	'Class:cmdbAbstractObject/Method:SetCurrentUser' => 'SetCurrentUser (initialiser à l\'utilisateur courant)',
 	'Class:cmdbAbstractObject/Method:SetCurrentUser+' => 'Initialiser un champ avec l\'utilisateur qui est en train d\'effectuer une action sur l\'objet',
 	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1' => 'Champ Cible',
-	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1+' => 'Le champ à initialiser, dans l\'objet courant',
+	'Class:cmdbAbstractObject/Method:SetCurrentUser/Param:1+' => 'Le champ à initialiser, dans l\'objet courant. Si ce champ est une chaîne de caractère, alors le nom usuel sera utilisé. Dans les autres cas, ce sera l\'identifiant de l\'objet. Le nom usuel est le nom usuel de la personne attachée au compte utilisateur. Si aucune personne n\'est rattachée au compte utilisateur, alors le nom usuel est l\'identifiant de connexion.',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson' => 'SetCurrentPerson (initialiser à l\'utilisateur courant)',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson+' => 'Initialiser un champ avec la personne associée au compte de l\'utilisateur qui est en train d\'effectuer une action sur l\'objet',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson/Param:1' => 'Champ Cible',
+	'Class:cmdbAbstractObject/Method:SetCurrentPerson/Param:1+' => 'Le champ à initialiser, dans l\'objet courant. Si ce champ est une chaîne de caractère, alors le nom usuel sera utilisé. Dans les autres cas, ce sera l\'identifiant de l\'objet',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime' => 'SetElapsedTime (initialiser avec le temps passé)',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime+' => 'Initialiser un champ avec la durée écoulée depuis une date donnée par un autre champ (champ de référence)',
 	'Class:cmdbAbstractObject/Method:SetElapsedTime/Param:1' => 'Champ Cible',