Commit 3ef522d8 authored by Frank Bergmann's avatar Frank Bergmann

- Core:

  Updated a sample Oracle import script to add users
  to a specific company
parent f6530fdd
......@@ -4,28 +4,26 @@
#
# import-oracle-users.perl
#
# Import users from an Oracle oracle database into ]po[
# (c) 2011 ]project-open[, frank.bergmann@project-open.com
# Import users from an Oracle into ]po[
# (c) 2006 ]project-open[, frank.bergmann@project-open.com
# All rights reserved.
#
# The import uses the SQLPLUS Oracle client to select out
# a number of fields about every users and uses PostgreSQL
# SQL statements in order to create the user on the ]po[
# side.
# --------------------------------------------------------
my $debug = 1;
# Oracle connection parameters
my $ora_user = "<project_open_user>\@<oracle_instance>";
my $ora_pwd = "<project_open_password>";
my $ora_user = "<user>@<instance>";
my $ora_pwd = "<passwd>";
# PostgreSQL connection parameters
my $pg_pwd = "";
my $pg_datasource = "dbi:Pg:dbname=projop";
my $pg_username = "projop";
my $logfile = "/var/log/import-oracle-users.log";
# --------------------------------------------------------
# Load libraries
......@@ -37,6 +35,13 @@ use IPC::Open2;
use DBI;
# --------------------------------------------------------
# Create a log file for the Oracle output
open(L, ">> $logfile") || die "import-oracle-users.perl: Couldn't open $logfile";
print L "import-oracle-users.perl: Starting Import\n";
# --------------------------------------------------------
# Establish the database connection and
# extract some constants.
......@@ -47,25 +52,37 @@ my $dbh = DBI->connect($pg_datasource, $pg_username, $pg_pwd) ||
my $registered_users_group_id = -2;
# --------------------------------------------------------
# Get the ID of the group "Customers"
#
my $sth = $dbh->prepare("SELECT group_id from groups where group_name = 'Customers'");
$sth->execute() || die "Error: import-oracle-users.perl: 2: Unable to execute SQL statement.\n";
my $row = $sth->fetchrow_hashref;
my $customer_group_id = $row->{group_id};
# --------------------------------------------------------
# Get the company_id of a specific customer
#
my $sth = $dbh->prepare("SELECT company_id from im_companies where company_path = '<company_path>'");
$sth->execute() || die "Error: import-oracle-users.perl: 2a: Unable to execute SQL statement.\n";
my $row = $sth->fetchrow_hashref;
my $customer_id = $row->{company_id};
# --------------------------------------------------------
# Execute the sqlplus client and return the list of records.
# Oracle needs these variables:
# export LD_LIBRARY_PATH=.
# export ORACLE_HOME=.
# export TNS_ADMIN=.
# Execute the sqlplus client and
# return the list of records
#
# Oracle needs these variables
# export LD_LIBRARY_PATH=.
# export ORACLE_HOME=.
# export TNS_ADMIN=.
local (*Reader, *Writer);
my $pid = open2(\*Reader, \*Writer, "/usr/lib/oracle/11.2/client/bin/sqlplus -s '$ora_user/$ora_pwd'");
# Tell Oracle to output clean text without formatting.
print Writer "set colsep ','\n";
print Writer "set sqlprompt ''\n";
print Writer "set echo off\n";
......@@ -76,33 +93,34 @@ print Writer "set headsep off\n";
print Writer "set linesize 1000\n";
print Writer "set pagesize 0\n";
# The Oracle SQL Statemente. Please replace with
# your equivalente column names. We separate columns
# with "\t" (tab) characters, which are easy to parse.
print Writer "
SELECT trim(USER_NR) || '\t' ||
FIRST_NAMES || '\t' ||
LAST_NAME || '\t' ||
TELEPHONE || '\t' ||
MOBILE_PHONE || '\t' ||
SELECT trim(USUARIO) || '\t' ||
NOMBRE || '\t' ||
APELLIDOS || '\t' ||
TELEFONO || '\t' ||
MOVIL || '\t' ||
EMAIL || '\t' ||
COMPANY || '\t' ||
to_char(DISABLE_DATE, 'YYYY-MM-DD')
FROM USERS_VIEW_PO;
COOPERATIVA || '\t' ||
to_char(FEC_BAJA, 'YYYY-MM-DD')
FROM SGEN_V_USUARIOS_PO;
";
print Writer "exit\n";
# We have to close the writer before we start reading
# We have to close the Writer before reading
close Writer;
# --------------------------------------------------------
# Loop through Oracle results and insert into ]po[
# Loop through Oracle results and
# insert into ]po[
my $line;
my $ctr = 0;
while ($line = <Reader>){
# Write Oracle output to logfile
print L $line;
# --------------------------------------------------------
# Decompose the line into several variables
......@@ -115,21 +133,25 @@ while ($line = <Reader>){
# Check completeness
#
if ("" eq $user_first_names) {
print "Warning: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has empty first_names, skipping\n";
print "Error: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has empty first_names, skipping\n";
next;
}
if ("" eq $user_last_name) {
print "Warning: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has empty last_name, skipping\n";
print "Error: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has empty last_name, skipping\n";
next;
}
if ("" eq $user_nr) {
print "Warning: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has no user_nr, skipping\n";
print "Error: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has no user_nr, skipping\n";
next;
}
# --------------------------------------------------------
# Complete missing fields
#
# Save the original email for reference
my $sisla_email = $user_email;
if ("" eq $user_email) {
print "Warning: import-oracle-users.perl: User '$user_first_names' '$user_last_name' ($user_nr) has no email, creating dummy email\n";
$user_email = "$user_first_names.$user_last_name\@lagunaro.es";
......@@ -143,10 +165,6 @@ while ($line = <Reader>){
my $row = $sth->fetchrow_hashref;
my $user_id = $row->{user_id};
# --------------------------------------------------------
# Create a new user if necessary
#
if ("" == $user_id) {
print "Notice: import-oracle-users.perl: Creating new user\n" if $debug;
$sth = $dbh->prepare("
......@@ -168,10 +186,6 @@ while ($line = <Reader>){
$sth = $dbh->prepare("INSERT INTO im_employees (employee_id) VALUES ($user_id)");
$sth->execute() || die "Error: import-oracle-users.perl: 6: Unable to execute SQL statement.\n";
# Add the original email to a DynField in table "persons"
$sth = $dbh->prepare("UPDATE persons SET sisla_email = '$user_email' where person_id = $user_id");
$sth->execute() || die "Error: import-oracle-users.perl: 6: Unable to execute SQL statement.\n";
# make the user an approved member of "registered_users"
my $sth = $dbh->prepare("SELECT count(*) as exists_p from acs_rels where object_id_one = $registered_users_group_id and object_id_two = $user_id and rel_type = 'membership_rel'");
$sth->execute() || die "Error: import-oracle-users.perl: 7a: Unable to execute SQL statement.\n";
......@@ -183,6 +197,11 @@ while ($line = <Reader>){
}
}
# Add the original email to a DynField in table "persons"
$sth = $dbh->prepare("UPDATE persons SET sisla_email = '$sisla_email' where person_id = $user_id");
$sth->execute() || die "Error: import-oracle-users.perl: 6: Unable to execute SQL statement.\n";
if ("" eq $user_id) {
print "Error: import-oracle-users.perl: Found empty user_id for user_nr=$user_nr.\n";
next;
......@@ -209,6 +228,28 @@ while ($line = <Reader>){
}
# --------------------------------------------------------
# Check if the user is member of his company already and make hima member otherwise
#
my $sth = $dbh->prepare("
SELECT count(*) as exists_p
from acs_rels r,
im_biz_object_members bom
where r.rel_id = bom.rel_id and
r.object_id_one = $customer_id and
r.object_id_two = $user_id and
rel_type = 'membership_rel'
");
$sth->execute() || die "Error: import-oracle-users.perl: 9: Unable to execute SQL statement.\n";
my $row = $sth->fetchrow_hashref;
my $exists_p = $row->{exists_p};
print "Notice: import-oracle-users.perl: Customer membership exists_p=$exists_p\n" if $debug;
if (!$exists_p) {
$sth = $dbh->prepare("SELECT im_biz_object_member__new(null, 'im_biz_object_member', $customer_id, $user_id, 1300, 624, '0.0.0.0')");
$sth->execute() || die "Error: import-oracle-users.perl: 9a: Unable to execute SQL statement.\n";
}
# --------------------------------------------------------
# Update user information
print "Notice: import-oracle-users.perl: Updating users_contact for user_id=$user_id.\n";
......@@ -222,8 +263,36 @@ while ($line = <Reader>){
print "Notice: import-oracle-users.perl: End of processing user_id=$user_id\n" if $debug;
# --------------------------------------------------------
# User status = "deleted" if the $user_disable_date is set
# -2 represents the "Registered Users" magic object
#
my $member_state = "approved";
if ("" ne $user_disable_date) {
$member_state = "banned";
}
print "Notice: import-oracle-users.perl: Updating user_status\n";
$sth = $dbh->prepare("
UPDATE membership_rels
SET member_state = '$member_state'
WHERE rel_id in (
select rel_id
from acs_rels r
where object_id_one = -2 and
object_id_two = $user_id and
rel_type = 'membership_rel'
)
");
$sth->execute() || die "Error: import-oracle-users.perl: 10: Unable to execute SQL statement.\n";
$ctr = $ctr + 1;
}
# Close Logfile
close(L);
exit 0;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment