diff --git a/htdocs/install/mysql/migration/3.0.0-3.1.0.sql b/htdocs/install/mysql/migration/3.0.0-3.1.0.sql
index 16d03bac7586fa7bb5e1ea990ced845755bf7b02..53d49471dd01a33ce89ea3e1037f093a01cd3445 100755
--- a/htdocs/install/mysql/migration/3.0.0-3.1.0.sql
+++ b/htdocs/install/mysql/migration/3.0.0-3.1.0.sql
@@ -105,6 +105,12 @@ ALTER TABLE llx_facturedet ADD INDEX idx_facturedet_fk_product (fk_product);
 ALTER TABLE llx_mailing_cibles ADD COLUMN tag varchar(128) NULL AFTER other;
 ALTER TABLE llx_mailing ADD COLUMN tag varchar(128) NULL AFTER email_errorsto;
 
+ALTER TABLE llx_usergroup_user DROP INDEX fk_user;
+ALTER TABLE llx_usergroup_user ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER rowid;
+ALTER TABLE llx_usergroup_user ADD UNIQUE INDEX uk_usergroup_entity (entity,fk_user,fk_usergroup);
+ALTER TABLE llx_usergroup_user ADD CONSTRAINT fk_usergroup_user_fk_user      FOREIGN KEY (fk_user)         REFERENCES llx_user (rowid);
+ALTER TABLE llx_usergroup_user ADD CONSTRAINT fk_usergroup_user_fk_usergroup FOREIGN KEY (fk_usergroup)    REFERENCES llx_usergroup (rowid);
+
 --Add Chile data (id pays=67)
 -- Regions Chile
 INSERT INTO llx_c_regions (rowid, code_region, fk_pays, cheflieu, tncc, nom, active) VALUES (6701, 6701, 67, NULL, NULL, 'Tarapacá', 1);
diff --git a/htdocs/install/mysql/tables/llx_usergroup_user.key.sql b/htdocs/install/mysql/tables/llx_usergroup_user.key.sql
new file mode 100644
index 0000000000000000000000000000000000000000..08dd60b186cec8ffc181a85250c7e44038756748
--- /dev/null
+++ b/htdocs/install/mysql/tables/llx_usergroup_user.key.sql
@@ -0,0 +1,24 @@
+-- ============================================================================
+-- Copyright (C) 2011 Regis Houssin  <regis@dolibarr.fr>
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with this program; if not, write to the Free Software
+-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+--
+-- $Id$
+-- ===========================================================================
+
+ALTER TABLE llx_usergroup_user ADD UNIQUE INDEX uk_user_group_entity (entity,fk_user,fk_usergroup);
+
+ALTER TABLE llx_usergroup_user ADD CONSTRAINT fk_usergroup_user_fk_user      FOREIGN KEY (fk_user)         REFERENCES llx_user (rowid);
+ALTER TABLE llx_usergroup_user ADD CONSTRAINT fk_usergroup_user_fk_usergroup FOREIGN KEY (fk_usergroup)    REFERENCES llx_usergroup (rowid);
diff --git a/htdocs/install/mysql/tables/llx_usergroup_user.sql b/htdocs/install/mysql/tables/llx_usergroup_user.sql
index d740e03e24c027e67a393428e21c66b910a9b984..98c56c37567323a7182c68071dd0be8dfa6cd87e 100644
--- a/htdocs/install/mysql/tables/llx_usergroup_user.sql
+++ b/htdocs/install/mysql/tables/llx_usergroup_user.sql
@@ -1,5 +1,6 @@
 -- ============================================================================
 -- Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
+-- Copyright (C) 2011 Regis Houssin        <regis@dolibarr.fr>
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -21,8 +22,8 @@
 create table llx_usergroup_user
 (
   rowid         integer AUTO_INCREMENT PRIMARY KEY,
+  entity        integer DEFAULT 1 NOT NULL,			-- multi company id
   fk_user       integer NOT NULL,
-  fk_usergroup  integer NOT NULL,
+  fk_usergroup  integer NOT NULL
 
-  UNIQUE(fk_user,fk_usergroup)
 )ENGINE=innodb;
diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php
index db03d310e50653ca6e13e3089bd3c3eb1bef9a84..7d0970b5e2915e5ec842eedcce4144351b4f1f64 100644
--- a/htdocs/user/class/user.class.php
+++ b/htdocs/user/class/user.class.php
@@ -4,7 +4,7 @@
  * Copyright (c) 2004-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
  * Copyright (C) 2004      Sebastien Di Cintio  <sdicintio@ressource-toi.org>
  * Copyright (C) 2004      Benoit Mortier       <benoit.mortier@opensides.be>
- * Copyright (C) 2005-2010 Regis Houssin        <regis@dolibarr.fr>
+ * Copyright (C) 2005-2011 Regis Houssin        <regis@dolibarr.fr>
  * Copyright (C) 2005      Lionel Cousteix      <etm_ltd@tiscali.co.uk>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -1465,34 +1465,38 @@ class User extends CommonObject
 
 
 	/**
-	 *    \brief      Add user into a group
-	 *    \param      group       id du groupe
+	 *    Add user into a group
+	 *    @param      group       id du groupe
 	 */
 	function SetInGroup($group)
 	{
+		global $conf;
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
-		$sql .= " WHERE fk_user  = ".$this->id;
-		$sql .= " AND fk_usergroup = ".$group;
+		$sql.= " WHERE fk_user  = ".$this->id;
+		$sql.= " AND fk_usergroup = ".$group;
+		$sql.= " AND entity = ".$conf->entity;
 
 		$result = $this->db->query($sql);
 
-		$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_user (fk_user, fk_usergroup)";
-		$sql .= " VALUES (".$this->id.",".$group.")";
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_user (entity, fk_user, fk_usergroup)";
+		$sql.= " VALUES (".$conf->entity.",".$this->id.",".$group.")";
 
 		$result = $this->db->query($sql);
 	}
 
 	/**
-	 *    \brief      Remove a user from a group
-	 *    \param      group       id du groupe
+	 *    Remove a user from a group
+	 *    @param      group       id du groupe
 	 */
 	function RemoveFromGroup($group)
 	{
+		global $conf;
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
-		$sql .= " WHERE fk_user  = ".$this->id;
-		$sql .= " AND fk_usergroup = ".$group;
+		$sql.= " WHERE fk_user  = ".$this->id;
+		$sql.= " AND fk_usergroup = ".$group;
+		$sql.= " AND entity = ".$conf->entity;
 
 		$result = $this->db->query($sql);
 	}
diff --git a/htdocs/user/class/usergroup.class.php b/htdocs/user/class/usergroup.class.php
index 114e9e1ca7cf7e7f0fc225eeb8412ee58c6b77f2..a5e7bca3b03ce426103d9987567210a8702f0dd5 100644
--- a/htdocs/user/class/usergroup.class.php
+++ b/htdocs/user/class/usergroup.class.php
@@ -1,6 +1,7 @@
 <?php
 /* Copyright (c) 2005      Rodolphe Quiedeville <rodolphe@quiedeville.org>
  * Copyright (c) 2005-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (c) 2005-2011 Regis Houssin        <regis@dolibarr.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -494,7 +495,7 @@ class UserGroup extends CommonObject
 
 		$now=dol_now();
 
-		$sql = "INSERT into ".MAIN_DB_PREFIX."usergroup (datec, nom, entity)";
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup (datec, nom, entity)";
 		$sql.= " VALUES('".$this->db->idate($now)."','".$this->db->escape($this->nom)."',".$conf->entity.")";
 
 		dol_syslog("UserGroup::Create sql=".$sql, LOG_DEBUG);