Menu

Limit search to current user??

Anonymous
2011-04-29
2012-12-19
  • Anonymous

    Anonymous - 2011-04-29

    Hi!

    I am  new to iTop, and have created a custom module.
    This module contains some global data and some personal data.

    The way this has been done is to create a abstract superclass with a personal subclass and  a global subclass.

    Now I am struggling to limit the results from the personal class to display content belonging to the current user only.

    Can anybody provide me with som info on:
    1. How to add data so that it will be "owned" by the current user. I.e. the user that enters the data.
    I have MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"Person", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT Person AS p WHERE p.id = :current_contact_id'), "sql"=>"user_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array())));

    2. How restrict viewing of these data to the user who entered them?

    Regards,

    Robert

     
  • Anonymous

    Anonymous - 2011-05-03

    In my quest to solve this, I have tried several alternatives.
    I cannot add the current_contact_id to my object. When I run the query in iTops "Run Queries" page, it returns the result I am looking for.

    What is wrong with this statement:?
    MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT User WHERE User.contactid = :this->contact_id'), "sql"=>"user_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array("contactid"))));

     
  • Romain Quetiez

    Romain Quetiez - 2011-05-03

    Hi Robban,

    The very first option that you have tested should work fine:

    SELECT ... WHERE ... = :current_contact_id
    

    will actually take the current user into account.

    Please note that if you are attempting to overload the attribute definition made in a class the current class is inherited from, then you might use the verb

    MetaModel::Init_OverloadAttributeParams($sAttCode, $aParams)
    

    .

    Did I understand you need?

    Regards,
    Romain

     
  • Anonymous

    Anonymous - 2011-05-19

    Hi!

    Thanks for the reply. However, it does not work. The value ":current_contact_id" is always empty.
    I am planning on using this information to filter the passwords so that the user will always see his own set of passwords.

    (The module is a simple password manager).

    This brings me to the next question. How to output the results, and restricted the output to passwords belonging to the current user.

    Thanks in advance.
    See the code below:

    abstract class Password extends cmdbAbstractObject
    {
        public static function Init()
        {
            $aParams = array
            (
                "category" => "bizmodel,searchable,configmanagment",
                "key_type" => "autoincrement",
                "name_attcode" => "password",
                "state_attcode" => "",
                "reconc_keys" => array("username"),
                "db_table" => "password",
                "db_key_field" => "id",
                "db_finalclass_field" => "",
                "display_template" => "",
            );
            MetaModel::Init_Params($aParams);
            MetaModel::Init_InheritAttributes();
    
            //if(!UserRights::IsLoggedIn()) echo "Ikke logget inn";
            MetaModel::Init_AddAttribute(new AttributeString("username", array("allowed_values"=>null, "sql"=>"username", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
            MetaModel::Init_AddAttribute(new AttributeString("password", array("allowed_values"=>null, "sql"=>"password", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
            MetaModel::Init_AddAttribute(new AttributeText("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
            MetaModel::Init_AddAttribute(new AttributeDate("expiry_date", array("allowed_values"=>null, "sql"=>"expiry_date", "default_value"=>"2099-12-31", "is_null_allowed"=>false, "depends_on"=>array())));
            MetaModel::Init_AddAttribute(new AttributeExternalKey("ci_id", array("targetclass"=>"FunctionalCI", "jointype"=>null, "allowed_values"=>null, "sql"=>"ci_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
            MetaModel::Init_SetZListItems('details', array('username', 'password', 'description', 'expiry_date', 'ci_id'));
            MetaModel::Init_SetZListItems('advanced_search', array('username', 'password', 'expiry_date'));
            MetaModel::Init_SetZListItems('standard_search', array('username', 'password', 'expiry_date'));
            MetaModel::Init_SetZListItems('list', array('username', 'password','expiry_date'));
        }
    }
    class PersonalPassword extends Password
    {
        public static function Init()
        {
            $aParams = array
            (
                "category" => "bizmodel,searchable,configmanagment",
                "key_type" => "autoincrement",
                "name_attcode" => "username",
                "state_attcode" => "",
                "reconc_keys" => array("username"),
                "db_table" => "password_personal",
                "db_key_field" => "id",
                "db_finalclass_field" => "",
                "display_template" => "",
            );
            MetaModel::Init_Params($aParams);
            MetaModel::Init_InheritAttributes();
    
            MetaModel::Init_AddAttribute(new AttributeExternalKey("user_id", array("targetclass"=>"User", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT User WHERE User.contactid = :current_contact_id'), "sql"=>"user_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array('contactid'))));
    
            //MetaModel::Init_AddAttribute(new AttributeExternalKey("servicesubcategory_id", array("targetclass"=>"ServiceSubcategory", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT ServiceSubcategory WHERE service_id = :this->service_id'), "sql"=>"servicesubcategory_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array("service_id"))));
    
            MetaModel::Init_SetZListItems('details', array('username', 'password', 'description', 'expiry_date', 'ci_id'));
            MetaModel::Init_SetZListItems('advanced_search', array('username', 'password', 'expiry_date'));
            MetaModel::Init_SetZListItems('standard_search', array('username', 'password', 'expiry_date'));
            MetaModel::Init_SetZListItems('list', array('username', 'password','expiry_date'));
        }
    
     
  • Romain Quetiez

    Romain Quetiez - 2011-05-31

    Hi,

    Please check that the login that you are using for testing is actually refering to a Contact. This is not mandatory in iTop.

    If this is not the case, then the value of current_contact_id is 0 (zero). I have just tested that with the following query:

    SELECT Server WHERE name LIKE CONCAT('%', :current_contact_id, '%')
    
     
  • Anonymous

    Anonymous - 2011-08-16

    Hi!

    Sorry for the long delay here.
    The login that I am using is referring to a contact.

    MetaModel::Init_AddAttribute(new AttributeExternalKey("contact_id", array("targetclass"=>"Contact", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT Contact WHERE Contact.id = :current_contact_id'), "sql"=>"contact_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
    

    The query

    SELECT Contact WHERE Contact.id = :current_contact_id
    

    does return my contact info when trying this query in the "Run queries" page. So it works, just not in my module.

    Any idea why?

    Br
    RObert

     
  • Anonymous

    Anonymous - 2011-10-19

    After a chat with Denis, its works.
    The solution was quite simple:
    The code:
    MetaModel::Init_AddAttribute(new AttributeExternalKey("contact_id", array("targetclass"=>"Person", "jointype"=>null, "allowed_values"=>new ValueSetObjects('SELECT Person WHERE id = :current_contact_id'), "sql"=>"contact_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));

    Now the important part, add this to the zList:
    MetaModel::Init_SetZListItems('details', array('username', 'password', 'contact_id','description', 'expiry_date', 'ci_id'));

    Voila, it works.

    Now its time to figure out how to manipulate the HTML output!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.