Example #1
0
/**
* \brief Function to get a certain user account property
* \param machineId The machine identifier of machine on which the user have a account
* \param property The property name
* \return the user account login
*/
std::string
UserServer::getUserAccountProperty(const std::string& machineId, const std::string& property) {

  init();

  std::string userId = getData().getUserId();
  UMS_Data::LocalAccount_ptr account = new UMS_Data::LocalAccount();
  account->setMachineId(machineId);
  account->setUserId(userId);
  LocalAccountServer localAccount(account, *msessionServer);

  UMS_Data::Machine_ptr machine = new UMS_Data::Machine();
  machine->setMachineId(machineId);
  MachineServer machineServer(machine);

  //To get the database number id of the machine
  std::string numMachine = machineServer.getAttribut("where machineid='"+localAccount.getData()->getMachineId()+"'");
  //To get the database number id of the user
  std::string numUser = getAttribut("where userid='"+localAccount.getData()->getUserId()+"'");

  std::string value;
  if ((numMachine.size() > 0) && (numUser.size() > 0)) {
	  value = localAccount.getAttribut("where machine_nummachineid="+numMachine+" and users_numuserid="+numUser, property);
  }

  if(value.size()==0) {
    delete account;
    delete machine;
    throw UMSVishnuException(ERRCODE_UNKNOWN_LOCAL_ACCOUNT, "You have not a local account on this machine");
  }

  delete account;
  delete machine;
  return value;
}
  /**
   * \brief Function to list locoal accounts information
   * \return The pointer to the UMS_Data::ListLocalAccounts containing local accounts information
   * \return raises an exception on error
   */
  UMS_Data::ListLocalAccounts* list(UMS_Data::ListLocalAccOptions_ptr option)
  {
    std::string query = boost::str(boost::format("SELECT DISTINCT machineid, userid, aclogin, home"
                                                 " FROM account, machine, users"
                                                 " WHERE account.status               = %1%"
                                                 "   AND machine.status               = %1%"
                                                 "   AND users.status                 = %1%"
                                                 "   AND account.machine_nummachineid = machine.nummachineid"
                                                 "   AND account.users_numuserid      = users.numuserid"
                                                 ) % vishnu::STATUS_ACTIVE);


    std::vector<std::string>::iterator dbResultIter;
    std::vector<std::string> dbResults;
    UMS_Data::UMS_DataFactory_ptr ecoreFactory = UMS_Data::UMS_DataFactory::_instance();
    mlistObject = ecoreFactory->createListLocalAccounts();

    //Creation of the object user
    UserServer userServer = UserServer(msessionServer);
    userServer.init();
    if (! userServer.exist()) {
      throw UMSVishnuException (ERRCODE_UNKNOWN_USER);
    }

    //To process options
    processOptions(userServer, option, query);

    boost::scoped_ptr<DatabaseResult> resultAccount(mdatabase->getResult(query));
    if (resultAccount->getNbTuples() != 0){
      for (size_t i = 0; i < resultAccount->getNbTuples(); ++i) {
        dbResults.clear();
        dbResults = resultAccount->get(i);
        dbResultIter = dbResults.begin();

        UMS_Data::LocalAccount_ptr localAccount = ecoreFactory->createLocalAccount();
        localAccount->setMachineId(*dbResultIter);
        localAccount->setUserId(*(++dbResultIter));
        localAccount->setAcLogin(*(++dbResultIter));
        localAccount->setHomeDirectory(*(++dbResultIter));

        mlistObject->getAccounts().push_back(localAccount);
      }
    }

    return mlistObject;
  }