diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-10-12 14:44:28 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-10-12 14:44:28 -0400 |
commit | e515f37435db6cf883215f13f02391d8b2107d47 (patch) | |
tree | 3df01bc0c420a2c497302c5a38aa9d724b2e764c /app/Model | |
parent | e57386a18393e85f073cccff70699ad9afc19119 (diff) |
Add user CSV import
Diffstat (limited to 'app/Model')
-rw-r--r-- | app/Model/Acl.php | 2 | ||||
-rw-r--r-- | app/Model/UserImport.php | 110 |
2 files changed, 112 insertions, 0 deletions
diff --git a/app/Model/Acl.php b/app/Model/Acl.php index 675ca36e..d05e4f77 100644 --- a/app/Model/Acl.php +++ b/app/Model/Acl.php @@ -88,6 +88,7 @@ class Acl extends Base */ private $admin_acl = array( 'user' => array('index', 'create', 'save', 'remove', 'authentication'), + 'userimport' => '*', 'config' => '*', 'link' => '*', 'currency' => '*', @@ -117,6 +118,7 @@ class Acl extends Base */ public function matchAcl(array $acl, $controller, $action) { + $controller = strtolower($controller); $action = strtolower($action); return isset($acl[$controller]) && $this->hasAction($action, $acl[$controller]); } diff --git a/app/Model/UserImport.php b/app/Model/UserImport.php new file mode 100644 index 00000000..afae0a48 --- /dev/null +++ b/app/Model/UserImport.php @@ -0,0 +1,110 @@ +<?php + +namespace Model; + +use SimpleValidator\Validator; +use SimpleValidator\Validators; +use Core\Csv; + +/** + * User Import + * + * @package model + * @author Frederic Guillot + */ +class UserImport extends Base +{ + /** + * Number of successful import + * + * @access public + * @var integer + */ + public $counter = 0; + + /** + * Get mapping between CSV header and SQL columns + * + * @access public + * @return array + */ + public function getColumnMapping() + { + return array( + 'username' => 'Username', + 'password' => 'Password', + 'email' => 'Email', + 'name' => 'Full Name', + 'is_admin' => 'Administrator', + 'is_project_admin' => 'Project Administrator', + 'is_ldap_user' => 'Remote User', + ); + } + + /** + * Import a single row + * + * @access public + * @param array $row + * @param integer $line_number + */ + public function import(array $row, $line_number) + { + $row = $this->prepare($row); + + if ($this->validateCreation($row)) { + if ($this->user->create($row)) { + $this->logger->debug('UserImport: imported successfully line '.$line_number); + $this->counter++; + } + else { + $this->logger->error('UserImport: creation error at line '.$line_number); + } + } + else { + $this->logger->error('UserImport: validation error at line '.$line_number); + } + } + + /** + * Format row before validation + * + * @access public + * @param array $data + * @return array + */ + public function prepare(array $row) + { + $row['username'] = strtolower($row['username']); + + foreach (array('is_admin', 'is_project_admin', 'is_ldap_user') as $field) { + $row[$field] = csv::getBooleanValue($row[$field]); + } + + $this->removeEmptyFields($row, array('password', 'email', 'name')); + + return $row; + } + + /** + * Validate user creation + * + * @access public + * @param array $values + * @return boolean + */ + public function validateCreation(array $values) + { + $v = new Validator($values, array( + new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50), + new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), User::TABLE, 'id'), + new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6), + new Validators\Email('email', t('Email address invalid')), + new Validators\Integer('is_admin', t('This value must be an integer')), + new Validators\Integer('is_project_admin', t('This value must be an integer')), + new Validators\Integer('is_ldap_user', t('This value must be an integer')), + )); + + return $v->execute(); + } +} |