2009-07-09 18:34:34 +04:00
/* -------------------------------------------------------------------------- */
2010-02-22 20:00:30 +03:00
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
2009-07-09 18:34:34 +04: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 ;
/**
* The User Pool class . . . .
*/
class UserPool : public PoolSQL
{
public :
2010-04-05 02:07:31 +04:00
UserPool ( SqlDB * db ) ;
2009-07-09 18:34:34 +04:00
~ UserPool ( ) { } ;
/**
* Function to allocate a new User object
* @ param oid the id assigned to the User
* @ return 0 on success
*/
int allocate (
int * oid ,
string hostname ,
string password ,
2010-04-05 02:07:31 +04:00
bool enabled ) ;
2009-07-09 18:34:34 +04: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
* @ return a pointer to the Host , 0 if the User could not be loaded
*/
User * get (
int oid ,
bool lock )
{
User * user = static_cast < User * > ( PoolSQL : : get ( oid , lock ) ) ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04:00
return user ;
}
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04: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
*/
User * get (
string username ,
bool lock )
{
map < string , int > : : iterator index ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04:00
index = known_users . find ( username ) ;
if ( index ! = known_users . end ( ) )
{
return get ( ( int ) index - > second , lock ) ;
}
return 0 ;
}
2010-04-05 02:07:31 +04:00
/** Update a particular User
2009-07-09 18:34:34 +04:00
* @ param user pointer to User
* @ return 0 on success
*/
int update ( User * user )
{
2010-04-05 02:07:31 +04:00
return user - > update ( db ) ;
2009-07-09 18:34:34 +04:00
} ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04:00
/** Drops a user from the DB, the user mutex MUST BE locked
* @ param user pointer to User
*/
int drop ( User * user )
{
2010-04-06 01:34:09 +04:00
int rc = PoolSQL : : drop ( user ) ;
2010-04-05 02:07:31 +04:00
if ( rc = = 0 )
{
2009-07-09 18:34:34 +04:00
known_users . erase ( user - > get_username ( ) ) ;
2010-04-05 02:07:31 +04:00
}
2009-07-09 18:34:34 +04:00
return rc ;
} ;
/**
* Bootstraps the database table ( s ) associated to the User pool
*/
2010-04-05 02:07:31 +04:00
static void bootstrap ( SqlDB * _db )
2009-07-09 18:34:34 +04:00
{
User : : bootstrap ( _db ) ;
} ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04:00
/**
* Returns whether there is a user with given username / password or not
* @ param session , colon separated username and password string
* @ return - 1 if there is no such a user , uid of the user if it exists
*/
int authenticate ( string & session ) ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04: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
*/
2010-04-05 02:07:31 +04:00
int dump ( ostringstream & oss , const string & where ) ;
2009-07-09 18:34:34 +04:00
private :
/**
* Factory method to produce User objects
* @ return a pointer to the new User
*/
PoolObjectSQL * create ( )
{
return new User ;
} ;
2010-04-05 02:07:31 +04:00
2009-07-09 18:34:34 +04:00
/**
* This map stores the association between UIDs and Usernames
*/
map < string , int > known_users ;
2010-04-05 02:07:31 +04:00
/**
* Callback function to get output the user pool in XML format
* ( User : : dump )
* @ param _oss pointer to the output stream
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
int dump_cb ( void * _oss , int num , char * * values , char * * names ) ;
/**
* Callback function to build the knwon_user map ( User : : User )
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
int init_cb ( void * nil , int num , char * * values , char * * names ) ;
2009-07-09 18:34:34 +04:00
} ;
2010-04-05 02:07:31 +04:00
# endif /*USER_POOL_H_*/