Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions core/Api/UserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,52 @@ public function account_list($user_id, $query = array())
$filter['created_by'] = $user_id;
return Account::search($filter);
}

/**
* Import new users from into DB
*
* @url POST /users/csv
*/
public function import_csv($data = array(), $mime = 'text/csv')
{
$this->_authorize('user_create');
$csv_data = explode(PHP_EOL, $data);
$temp = array_pop($csv_data);

// skip header row
$header_csv = str_getcsv($csv_data[0], ",");
foreach(array_slice($csv_data, 1) as $row) {
$row_csv = str_getcsv($row, ",");
$user_data = array_combine($header_csv, $row_csv);

$oUser = new User();
$this->set($oUser, $user_data);
$oUser->__set('active', intval($user_data['active']));
if (!$oUser->save()) {
throw new CoreException(417, 'Could not save User');
}
}

/**
* Export all users form DB
*
* @url GET /users/csv
*/
public function export_csv()
{
$fields = array('username', 'passwd', 'first_name', 'last_name', 'email', 'address', 'company', 'active');
$query = 'SELECT ' . implode(", ", $fields) . ' FROM usr';
$result = DB::query('usr', $query);
$filepath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'users.csv';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use random file name, tempname function ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean by this. Could you please elaborate? thanks

$handle = fopen($filepath, 'w');
$head = implode(",", $fields) . "\n";
fwrite($handle, $head);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$csv = implode(",",$row) . "\n";
fwrite($handle, $csv);
}
fclose($handle);
return new SplFileInfo($filepath);
}
}
}
30 changes: 30 additions & 0 deletions docs/ApiGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,36 @@ list all exiting users, optionally client can filter users using query string (k

+ Attributes (User)

### Export All Users via CSV [/users/csv]

list all exiting users, optionally client can filter users using query string (key value pair) in url, while using any of following fields

+ Request

+ Headers

Authentication: Bearer JWT

+ Response 200 (text/csv)

+ csv (array[User])

### Import All Users via CSV [/users/csv]

list all exiting users, optionally client can filter users using query string (key value pair) in url, while using any of following fields

+ Request

+ Headers

Authentication: Bearer JWT
+ Body

"CSV file contents"
+ Response 200

+ Attributes (user)

## User Role Define [/users/{user_id}/roles/{role_id}]

+ Parameters
Expand Down
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ User Management
* PUT users/{user_id}/roles/{role\_id}
* DELETE users/{user_id}/roles/{role\_id}
* GET /users/{user_id}/accounts
* GET /users/csv
* POST /users/csv

Trunk / Termination Providers APIs
----------------------------------
Expand Down