# -*- coding: latin-1 -*-
""" 
   Copyright (C) 1999-2005 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""

from Products.PimenTechLibCommon.pgobject import PgObject

import DateTime

class UserId(PgObject):
	"""
	UserId object
	Uses first acl_users folder found by acquisition
	PgObject is the Top class 
	"""

	meta_type = "UserId"
	
	icon = "misc_/OFSP/UserFolder_icon.gif"
	
	tablename = "userid"
		
	__PgObject_MAA = PgObject.manage_afterAdd
	__PgObject_init = PgObject.__init__

	def __init__(self, id, connection_id, uid=None, roles = ('Anonymous',), initialized_from = 'postgresql'):
		self.roles = roles
		self.__PgObject_init(id, connection_id, uid, initialized_from)

	def manage_afterAdd(self, item, container):
		self.__PgObject_MAA(item, container)
		self.update()

	def set_user(self):
		" Add a user "
		login = self.get_row_attr('login')
		pwd = self.get_row_attr('pwd')
		roles = self.roles
		self.notice("Change or add user '%s' with roles %s" % (login, `roles`))
		if self.acl_users.getUser(login):
			return self.acl_users._changeUser(login, pwd, pwd, roles, 0)
		else:
			return self.acl_users._addUser(login, pwd, pwd, roles, 0)

	def del_user(self):
		" Remove a user "
		self.acl_users.acl_users_delUsers((self.get_row_attr('login'),))
		
	def update(self, REQUEST=None):
		" to be implemented "
		self.title = self.get_row_attr('login')
		self.set_user()

	def modify(self, REQUEST, roles=None):
		" set new content"
		form = REQUEST.form
		row = self[self.tablename]
		row['ref_statut'] = 0	
		for key, item in form['userid'].items():
			row[key] = item

		self.commit(REQUEST)
		if roles is not None:
			self.roles = roles
		self.set_user()
		self.reindex()
		
	def _suspend(self, REQUEST):
		row = self[self.tablename]
		row['ref_statut'] = 7
		self.commit(REQUEST)
		self.roles = ('Anonymous',)
		self.set_user()
		self.reindex()

class Profils(PgObject):
	" Update profild for a userid "
	
	tablename = "profils"
	meta_type = "Profils"

	
	def __init__(self, id, connection_id, uid=None, initialized_from = 'postgresql', profil=None):
		PgObject.__init__(self, id, connection_id, uid, initialized_from)
		self.profil = profil
		
	def manage_afterAdd(self, id, container):
		" Zope method : automaticaly called when an instance is created. "
		self._create_current_row()
		row = self[self.tablename]
		row['ref_userid'] = container['userid'].getUid()
		if self.profil:
			row['ref_profil'] = int(self.profil)
		
		PgObject.manage_afterAdd(self, id, container)
		self.reindex()

	def modify(self, REQUEST):
		" change profils "
		row = self[self.tablename]

		row['ref_profil'] = int(REQUEST.form['profil'])
		self.commit(REQUEST)
		self.reindex()

