Only access connected user's object

Discussion relating to version 1.2.x stable

Only access connected user's object

Postby Bloodkiller99 » Mon Jun 07, 2010 4:44 pm

Hello

I'm back with a new problem

my table ENTREPRISE is linked with SFGUARDUSER and my table JOBREQUEST is linked with ENTREPRISE !

I want when an user is connected and is an Entreprise he can see all the JOBREQUESTS he posted and not everything, I also want to know how to make only him can edit his JOBREQUESTS .

I didn't find any usefull information..

Thanks in advance
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby marcink86 » Mon Jun 07, 2010 6:18 pm

I'm not quite sure if I understood what you want to do but simple JOIN between these three tables in SQL statement should solve your problem.
marcink86
Member
 
Posts: 52
Joined: Fri Aug 28, 2009 1:57 am
Location: Gdynia, Poland

Re: Only access connected user's object

Postby Bloodkiller99 » Tue Jun 08, 2010 10:27 am

thanks for your answer

in fact i did the simple join ( i'm pretty sure ! )

here is my schema.yml

Code: Select all
Entreprise:
  tableName: entreprise
  columns:
    id_entreprise:
      type: integer(8)
      primary: true
      autoincrement: true
    id_sfguarduserent:
      type: integer(4)
      notnull: true
    nom:
      type: string(255)
    adresse:
      type: string(255)
    cp:
      type: integer(5)
    ville:
      type: string(255)
    email:
      type: string(255)
    urlimage:
      type: string(255)
    logo:
      type: string(255)
    created_at: timestamp(25)
    updated_at: timestamp(25)
  relations:
    Achat_Pass:
      local: id_entreprise
      foreign: entreprise_id
      type: many
    Achat_Ticket:
      local: id_entreprise
      foreign: entreprise_id
      type: many
    Annonce:
      local: id_entreprise
      foreign: entreprise_id
      type: many
    sfGuardUser:
      class: sfGuardUser
      local: id_sfguarduserent
      foreign: id
      foreignAlias: Entreprise
      foreignType: one

Annonce:
  tableName: annonce
  columns:
    id:
      type: integer(8)
      primary: true
      autoincrement: true
    libelle:
      type: string(255)
      notnull: true
    poste:
      type: string(255)
      notnull: true
    lieu:
      type: string(255)
      notnull: true
    salaire:
      type: string(255)
      notnull: true
    contact:
      type: string()
      notnull: true
    fonction:
      type: string()
      notnull: true
    secteur:
      type: string()
      notnull: true
    date_postage:
      type: date(25)
      notnull: true
    age_recherche:
      type: string(255)
      notnull: false
    confidentialitee:
      type: integer(1)
      default: '1'
      notnull: true
    entreprise_id:
      type: integer(8)
      notnull: true
    details: string()
    created_at: timestamp(25)
    updated_at: timestamp(25)
  relations:
    Entreprise:
      local: entreprise_id
      foreign: id_entreprise
      type: one

sfGuardUser:
  actAs: [Timestampable]
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(128)
      notnull: true
      unique: true
    algorithm:
      type: string(128)
      default: sha1
      notnull: true
    salt: string(128)
    password: string(128)
    is_active:
      type: boolean
      default: 1
    is_super_admin:
      type: boolean
      default: 0
    last_login:
      type: timestamp
  indexes:
    is_active_idx:
      fields: [is_active]
  relations:
    groups:
      class: sfGuardGroup
      local: user_id
      foreign: group_id
      refClass: sfGuardUserGroup
      foreignAlias: Users
    permissions:
      class: sfGuardPermission
      local: user_id
      foreign: permission_id
      refClass: sfGuardUserPermission
      foreignAlias: Users
    Demandeur:
      local: id
      foreign: id_sfguarduserdem
    Entreprise:
      local: id
      foreign: id_sfguarduserent



And if I use php my admin to export the sql generated by doctrine and inserted into the base to mysqlWorkbench it shows me a relation but i can't see if it's a one-one or one-many, moreover i can add many ENTREPRISE to one sf_Guard_user so i guess there is a problem but the schema looks good.

I also use a code that i get from http://redotheoffice.com/?p=42 that adds an not-filled Entreprise when i create a new SfUser


so why can i access to other Entreprises than only the one i created ?

and another question how can i do a link to the entreprise linked to the user ?

$sfuser->getentreprise() ?


Thanks in advance!
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby halfer » Tue Jun 08, 2010 10:44 am

Not really enough info here to go on. Do you want a 1:1 relation between sf_guard_user and entreprise? If so, entreprise could be made into a sfGuard profile table.

Also, what you have access to as a particular user, or user type, depends on how you have implemented editing records. Are you using the admin generator? I tend to just use the module generator (crud generator) and then add in a select where clause in the list, edit and save actions.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: Only access connected user's object

Postby Bloodkiller99 » Tue Jun 08, 2010 11:13 am

Thanks for your rapid reply

In fact i can't insert my entreprise fields into profile beacause i have 2 tables !

My user is, a DEMANDEUR or an ENTREPRISE

so when he fill the profile form i look if he is a DEMANDEUR or an ENTREPRISE ( I putted a checkbox )

so i'm obligated to have the 2 tables DEMANDEUR and ENTREPRISE

here is the EER diagram !

<img src="index.php/fa/1570/0/" border=0 alt="index.php/fa/1570/0/">

So i want a "SFGAURDUSER 0:1 - 1:1 ENTREPRISE/DEMANDEUR" relation between the tables but i don't know how to implement this
Attachments
base.png
base.png (178.25 KiB) Viewed 858 times
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby Bloodkiller99 » Tue Jun 08, 2010 12:25 pm

So i just checked my model and it is already good because in basesfguardUser.class.php i have:

Code: Select all
        $this->hasOne('Demandeur', array(
             'local' => 'id',
             'foreign' => 'id_sfguarduserdem'));

        $this->hasOne('Entreprise', array(
             'local' => 'id',
             'foreign' => 'id_sfguarduserent'));


and in baseentreprise.class.php i have:

Code: Select all
      $this->hasMany('Annonce', array(
             'local' => 'id_entreprise',
             'foreign' => 'entreprise_id'));

        $this->hasOne('sfGuardUser', array(
             'local' => 'id_sfguarduserent',
             'foreign' => 'id'));


and in baseDemandeur.class.php :

Code: Select all
    $this->hasOne('sfGuardUser', array(
             'local' => 'id_sfguarduserdem',
             'foreign' => 'id'));


so so it is a one to one relation !

but i can't use "
Code: Select all
$sf_user->getEntreprise()-getid();
" in the link !

Code: Select all
echo link_to("Show my entreprise", "uneEntreprise/show/id/$sf_user->getEntreprise()->getid()");


"uneEntreprise" is the name of my module !
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby halfer » Tue Jun 08, 2010 1:07 pm

I am not sure you can formally have a 1:1 relation between sf_guard_user and demandeur, or sf_guard_user and entreprise, as it is possible to have 0:1 in each case. However this may not stop it working.

I would add a "type" in sf_guard_user_profile to determine whether a user is a demandeur or an entreprise. Then, in your myUser class, which should extend sfGuardSecurityUser, you can do something like:

Code: Select all
public function getUserType()
{
  return $this->getProfile->getType();
}

In your action you would then do:

Code: Select all
public function execute($r)
{
  switch ($this->getUser()->getUserType())
  {
    case PROFILE_PEER::TYPE_DEMANDEUR:
      // Doctrine code to limit your select by demandeur
      break;
    case PROFILE_PEER::TYPE_ENTREPRISE:
      // Doctrine code to limit your select by entreprise
      break;
    default:
      // Doctrine code to return null result set
  }
}

Put in a suitable child Doctrine class for PROFILE_PEER - I am not sure what to put here, as I use Propel not Doctrine! But I am sure you can work that out. They just need to be constants so you can tell the difference between the two items.

You will also need to be able to recall the id_demandeur or id_entreprise for a user at any time. This could be done inside myUser again, with a new function, or do as I do and read it upon logon and store it in a persistent user attribute.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: Only access connected user's object

Postby Bloodkiller99 » Wed Jun 16, 2010 9:21 am

Sorry for answering so much time after.

Ok i did just as you write I've put a new field saying if you're a pro or not.

and i've resolved my problem to see The entreprise of one User by an DQL query which is:

Code: Select all
   
$this->entreprise_list = Doctrine::getTable('Entreprise')
   ->createQuery('ent')
   ->where('ent.id_sfguarduserent = ?', $this->getuser()->getGuardUser()->getid())
   ->execute();


But my new problem is to see all the onnonce of This entreprise and I can't manage how to get the entrepriseId of the only one that is returned by my query then reuse it in this query.

Code: Select all
         $this->annonce_list = Doctrine::getTable('Annonce')
           ->createQuery('ann')
           ->where('ann.entreprise_id = ?','ent.id_entreprise');


Thanks in advance
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby halfer » Wed Jun 16, 2010 12:11 pm

I didn't fully understand your last sentence, but it sounds like a Doctrine question. I am not a Doctrine user unfortunately :roll:

However, to bring us up-to-date with your progress, are you able to read the id_demandeur or id_entreprise of the user inside the switch statement?
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: Only access connected user's object

Postby Bloodkiller99 » Wed Jun 16, 2010 2:45 pm

I'm sorry for my english :oops:

I want to see al the JOBS(annonces in french) of one COMPANY(Entrepris in french).

And to find the company related to the connected user I used an DQl query.


Code: Select all
$this->entreprise_list = Doctrine::getTable('Entreprise')
   ->createQuery('ent')
   ->where('ent.id_sfguarduserent = ?', $this->getuser()->getGuardUser()->getid())
   ->execute();


It shows me the only Enterprise of that user.

And i am working on showing all the Jobs of that Company with this request but it doesn't work .
Code: Select all
         $this->annonce_list = Doctrine::getTable('Annonce')
           ->createQuery('ann')
           ->where('ann.entreprise_id = ?','ent.id_entreprise');


I think that this would do the job:
Code: Select all
SQL

Select * From ANNONCE
Where ANNONCE.entreprise_id = (the id of the only entreprise returned by the first DQL query request) ;



And waht do you mean by "inside the switch statement?"


thank you in advance
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby halfer » Wed Jun 16, 2010 3:23 pm

Don't worry about your English, it's all good practise! However it was also that you introduced a new topic, a "pro" field, which wasn't in your original post. Adding in new topics halfway through is just confusing.

"inside the switch statement": See the switch statement in my example code in my post at [Tue, 08 June 2010 12:07]? That's what I was referring to. As far as I can tell, you will have to execute a different statement, depending on the user type, in order to filter your resultset.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: Only access connected user's object

Postby Bloodkiller99 » Thu Jun 17, 2010 9:28 am

Okay i didn't understood "switch statement" was the name for a

Code: Select all
switch{
   case:---;
   case:---;

}



But I didn't used it ( my pro field) for anything else than say if this is a pro connected or say if it is a user that is registering.

The problem i have is to reuse the id of the one line DQL for the next one that show me all the jobs of one company.

But DQl isn't easy to understand. and I don't know how to translate from SQl to DQL..

Code: Select all
SQL

Select ANNONCE.name, ANNONCE.entreprise_id From ANNONCE,ENTREPRISE
Where ANNONCE.entreprise_id = ENTREPRISE.id_entreprise
AND ENTREPRISE.id_entreprise = (and there juste reuse the the only id returned by this DQL query )

[COLOR=orangered]$this->entreprise_list = Doctrine::getTable('Entreprise')
   ->createQuery('ent')
   ->where('ent.id_sfguarduserent = ?', $this->getuser()->getGuardUser()->getid())
   ->execute();[/COLOR]



I don't know to reuse the result ..

I tried .
$xx->entreprise_list = Doctrine::getTable('Entreprise')
->createQuery('ent')
->where('ent.id_sfguarduserent = ?', $this->getuser()->getGuardUser()->getid())
->execute();

then in my next query do it with

Code: Select all
AND ENTREPRISE.id_entreprise = $xx[1]


But it doesn't work..

I'm sure it's possible but it's like i can't find the simpliest way.
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby Bloodkiller99 » Thu Jun 17, 2010 11:01 am

Oh I realized something..

$this->getuser()->getGuardUser()->getProfile()->getid()

returns the id of the Profile !


How can i make a function

to do

$this->getuser()->getGuardUser()->getEntreprise()->getid()

returns the id of the Entreprise of the sf User ?

And where an i declare it ?
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse

Re: Only access connected user's object

Postby halfer » Thu Jun 17, 2010 11:04 am

Yes, that's correct - it will return the id of your profile. Hence the need for the switch block - you need to do a look up on one table or another depending on the type of the user, as I previously indicated.

Edit - this will require another select statement - I don't think this is possible to do in one Doctrine call, since the table involved is conditional on your user type.
Last edited by halfer on Thu Jun 17, 2010 11:12 am, edited 1 time in total.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: Only access connected user's object

Postby Bloodkiller99 » Thu Jun 17, 2010 11:23 am

the call to the function will be in the pro or not-pro action.class.php so no need for the switch .

I added this in
sfGuardSecurityUser.class.php
Code: Select all
  public function getEntreprise()
  {
    return $this->getGuardUser() ? $this->getGuardUser()->getEntreprise() : null;
  }
Last edited by Bloodkiller99 on Thu Jun 17, 2010 11:29 am, edited 1 time in total.
--------------------------------------
----------- 318 i - e30 ------------
--------------------------------------
Bloodkiller99
Junior Member
 
Posts: 17
Joined: Tue Apr 27, 2010 3:11 pm
Location: Toulouse


Return to symfony 1.2

Who is online

Users browsing this forum: No registered users and 0 guests