/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"

#include "wb_config.h"

#include "editor_user.h"
#include "db_object_helpers.h"

using namespace bec;
using namespace grt;

UserEditorBE::UserEditorBE(GRTManager *grtm, const db_UserRef &user)
: DBObjectEditorBE(grtm, user, db_mgmt_RdbmsRef()), _user(user), _role_tree(db_CatalogRef::cast_from(user->owner()))
{
}



void UserEditorBE::set_password(const std::string &pass)
{
  if (get_password() != pass)
  {
    AutoUndoEdit undo(this, get_user(), "password");
    
    get_user()->password(pass);
    
    update_change_date();
    undo.end(strfmt(_("Set Password for User '%s'"), get_user()->name().c_str()));
  }
}


std::string UserEditorBE::get_password()
{
  return get_user()->password();
}


RoleTreeBE *UserEditorBE::get_role_tree()
{
  return &_role_tree;
}

#include <iostream>

void UserEditorBE::add_role(const std::string &role_name)
{
  db_RoleRef role(grt::find_named_object_in_list(db_CatalogRef::cast_from(get_user()->owner())->roles(), role_name));

  if ( role.is_valid() && BaseListRef::npos == get_user()->roles().get_index(role) )
  {
    AutoUndoEdit undo(this);
    get_user()->roles().insert(role);
    update_change_date();
    undo.end(strfmt(_("Assign Role '%s' to User '%s'"), role_name.c_str(), get_name().c_str()));
  }
}


void UserEditorBE::remove_role(const std::string &role_name)
{
  db_RoleRef role(grt::find_named_object_in_list(db_CatalogRef::cast_from(get_user()->owner())->roles(), role_name));

  if (role.is_valid())
  {
    const size_t index = get_user()->roles().get_index(role);
    if ( index != BaseListRef::npos )
    {
      AutoUndoEdit undo(this);
      get_user()->roles().remove(index);
      update_change_date();
      undo.end(strfmt(_("Revoke Role '%s' from User '%s'"), role_name.c_str(), get_name().c_str()));
    }
  }
}


std::vector<std::string> UserEditorBE::get_roles()
{
  std::vector<std::string> roles;

  for (size_t c= get_user()->roles().count(), i= 0; i < c; i++)
    roles.push_back(*get_user()->roles()[i]->name());

  return roles;
}
