2009-07-09 14:34:34 +00:00
/* -------------------------------------------------------------------------- */
2011-02-25 14:34:44 +01:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
2009-07-09 14:34:34 +00:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef USER_POOL_H_
# define USER_POOL_H_
# include "PoolSQL.h"
# include "User.h"
# include <time.h>
# include <sstream>
# include <iostream>
# include <vector>
using namespace std ;
2010-07-08 19:45:00 +02:00
class AuthRequest ; //Forward definition of AuthRequest
2009-07-09 14:34:34 +00:00
/**
* The User Pool class . . . .
*/
class UserPool : public PoolSQL
{
public :
2010-04-28 18:37:45 +02:00
UserPool ( SqlDB * db ) ;
2009-07-09 14:34:34 +00:00
~ UserPool ( ) { } ;
/**
* Function to allocate a new User object
* @ param oid the id assigned to the User
2010-04-28 18:37:45 +02:00
* @ return the oid assigned to the object or - 1 in case of failure
2009-07-09 14:34:34 +00:00
*/
int allocate (
2010-08-05 19:28:28 +02:00
int * oid ,
string hostname ,
string password ,
bool enabled ,
string & error_str ) ;
2009-07-09 14:34:34 +00:00
/**
* Function to get a User from the pool , if the object is not in memory
* it is loaded from the DB
* @ param oid User unique id
* @ param lock locks the User mutex
2011-03-14 19:06:39 +01:00
* @ return a pointer to the User , 0 if the User could not be loaded
2009-07-09 14:34:34 +00:00
*/
2011-03-09 12:47:49 +01:00
User * get ( int oid , bool lock )
2009-07-09 14:34:34 +00:00
{
2011-03-09 12:47:49 +01:00
return static_cast < User * > ( PoolSQL : : get ( oid , lock ) ) ;
2011-03-14 19:06:39 +01:00
} ;
2009-07-09 14:34:34 +00:00
/**
* Function to get a User from the pool , if the object is not in memory
* it is loaded from the DB
* @ param username
* @ param lock locks the User mutex
* @ return a pointer to the User , 0 if the User could not be loaded
*/
2011-03-09 12:47:49 +01:00
User * get ( string name , bool lock )
2009-07-09 14:34:34 +00:00
{
2011-03-09 12:47:49 +01:00
return static_cast < User * > ( PoolSQL : : get ( name , - 1 , lock ) ) ;
2011-03-14 19:06:39 +01:00
} ;
2009-07-09 14:34:34 +00:00
2010-04-05 00:07:31 +02:00
/** Update a particular User
2009-07-09 14:34:34 +00:00
* @ param user pointer to User
* @ return 0 on success
*/
int update ( User * user )
{
2010-04-05 00:07:31 +02:00
return user - > update ( db ) ;
2009-07-09 14:34:34 +00:00
} ;
2010-04-05 00:07:31 +02:00
2009-07-09 14:34:34 +00:00
/** Drops a user from the DB, the user mutex MUST BE locked
* @ param user pointer to User
*/
int drop ( User * user )
{
2011-03-09 12:47:49 +01:00
return PoolSQL : : drop ( user ) ;
2009-07-09 14:34:34 +00:00
} ;
/**
* Bootstraps the database table ( s ) associated to the User pool
*/
2010-04-05 00:07:31 +02:00
static void bootstrap ( SqlDB * _db )
2009-07-09 14:34:34 +00:00
{
User : : bootstrap ( _db ) ;
} ;
2010-04-05 00:07:31 +02:00
2009-07-09 14:34:34 +00:00
/**
* Returns whether there is a user with given username / password or not
* @ param session , colon separated username and password string
2010-07-08 19:45:00 +02:00
* @ return - 1 if authn failed , uid of the user in other case
2009-07-09 14:34:34 +00:00
*/
int authenticate ( string & session ) ;
2010-04-05 00:07:31 +02:00
2010-07-08 19:45:00 +02:00
/**
* Returns whether there is a user with given username / password or not
* @ param ar , an Authorization Request
* @ return - 1 if authz failed , 0 otherwise
*/
2010-07-09 12:10:05 +02:00
static int authorize ( AuthRequest & ar ) ;
2010-07-08 19:45:00 +02:00
2009-07-09 14:34:34 +00:00
/**
* Dumps the User pool in XML format . A filter can be also added to the
* query
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
*/
2011-03-09 13:23:01 +01:00
int dump ( ostringstream & oss , const string & where )
{
return PoolSQL : : dump ( oss , " USER_POOL " , User : : table , where ) ;
2011-03-14 19:06:39 +01:00
} ;
2009-07-09 14:34:34 +00:00
private :
/**
* Factory method to produce User objects
* @ return a pointer to the new User
*/
PoolObjectSQL * create ( )
{
2011-03-09 12:47:49 +01:00
return new User ( - 1 , " " , " " , true ) ;
2009-07-09 14:34:34 +00:00
} ;
} ;
2010-09-02 20:44:14 +02:00
# endif /*USER_POOL_H_*/