* code for task#1580.

This commit is contained in:
xia0ta0
2013-07-19 14:40:51 +08:00
parent 36a90cd70d
commit 6954ee9d89
3 changed files with 319 additions and 11 deletions
+14
View File
@@ -51,6 +51,8 @@ $lang->moduleOrder[140] = 'tree';
$lang->moduleOrder[145] = 'api';
$lang->moduleOrder[150] = 'file';
$lang->moduleOrder[155] = 'misc';
$lang->moduleOrder[160] = 'sso';
$lang->moduleOrder[165] = 'webapp';
$lang->resource = new stdclass();
@@ -619,6 +621,18 @@ $lang->resource->admin->checkDB = 'checkDB';
$lang->admin->methodOrder[0] = 'index';
$lang->admin->methodOrder[5] = 'checkDB';
/* SSO. */
$lang->resource->sso = new stdclass();
$lang->resource->sso->browse = 'browse';
$lang->resource->sso->create = 'create';
$lang->resource->sso->edit = 'edit';
$lang->resource->sso->delete = 'delete';
$lang->sso->methodOrder[0] = 'browse';
$lang->sso->methodOrder[5] = 'create';
$lang->sso->methodOrder[10] = 'edit';
$lang->sso->methodOrder[15] = 'delete';
/* Extension. */
$lang->resource->extension = new stdclass();
$lang->resource->extension->browse = 'browse';
+132 -11
View File
@@ -1,21 +1,102 @@
<?php
class sso extends control
{
public function auth($key)
/**
* Browse all auths.
*
* @access public
* @return void
*/
public function browse()
{
if(!empty($_POST) or (isset($_GET['account']) and isset($_GET['password'])))
$this->view->title = $this->lang->sso->browse;
$this->view->auths = $this->sso->getAuths();
$this->display();
}
/**
* Create auth.
*
* @access public
* @return void
*/
public function create()
{
if(!empty($_POST))
{
$account = '';
$password = '';
if($this->post->account) $account = $this->post->account;
if($this->get->account) $account = $this->get->account;
if($this->post->password) $password = $this->post->password;
if($this->get->password) $password = $this->get->password;
if(!$this->post->title) die(js::alert($this->lang->sso->error->title));
if(!$this->post->code) die(js::alert($this->lang->sso->error->code));
if(!$this->post->ip) die(js::alert($this->lang->sso->error->ip));
$this->sso->createAuth();
if(dao::isError()) die(js::error(dao::getError()));
die(js::locate(inlink('browse'), 'parent'));
}
$this->view->title = $this->lang->sso->create;
$this->view->key = $this->sso->createKey();
$this->display();
}
/**
* Edit auth.
*
* @param string $code
* @access public
* @return void
*/
public function edit($code)
{
if(!empty($_POST))
{
if(!$this->post->title) die(js::alert($this->lang->sso->error->title));
if(!$this->post->ip) die(js::alert($this->lang->sso->error->ip));
$this->sso->updateAuth($code);
if(dao::isError()) die(js::error(dao::getError()));
die(js::locate(inlink('browse'), 'parent'));
}
$user = $this->loadModel('user')->identify($account, $password);
$this->view->auth = $this->sso->getAuth($code);
$this->view->code = $code;
$this->display();
}
/**
* Delete auth.
*
* @param string $code
* @param string $confirm
* @access public
* @return void
*/
public function delete($code, $confirm = 'no')
{
if($confirm == 'no')
{
die(js::confirm($this->lang->sso->confirmDelete, inlink('delete', "code=$code&confirm=yes")));
}
else
{
$this->sso->deleteAuth($code);
die(js::locate(inlink('browse'), 'parent'));
}
}
/**
* Auth user.
*
* @param string $app
* @access public
* @return void
*/
public function auth($app)
{
$user = $this->sso->identify($app);
if($user)
{
$dept = $this->loadModel('dept')->getByID($user->dept);
$user->deptName = $dept ? $dept->name : '';
$response['status'] = 'success';
$response['data'] = json_encode($user);
$this->send($response);
@@ -26,11 +107,51 @@ class sso extends control
$this->send($response);
}
public function depts($key)
/**
* Get all departments.
*
* @param string $app
* @access public
* @return void
*/
public function depts($app)
{
if($this->post->key) $key = $this->post->key;
if($this->get->key) $key = $this->get->key;
if($this->sso->checkIP($app) and $this->sso->getAppKey($app) == $key)
{
$depts = $this->sso->getAllDepts();
$response['status'] = 'success';
$response['data'] = json_encode($depts);
$this->send($response);
}
$response['status'] = 'fail';
$response['data'] = 'key error';
$this->send($response);
}
public function users($key)
/**
* Get all users.
*
* @param string $app
* @access public
* @return void
*/
public function users($app)
{
if($this->post->key) $key = $this->post->key;
if($this->get->key) $key = $this->get->key;
if($this->sso->checkIP($app) and $this->sso->getAppKey($app) == $key)
{
$depts = $this->sso->getAllUsers();
$response['status'] = 'success';
$response['data'] = json_encode($depts);
$this->send($response);
}
$response['status'] = 'fail';
$response['data'] = 'key error';
$this->send($response);
}
}
+173
View File
@@ -1,5 +1,178 @@
<?php
class ssoModel extends model
{
/**
* Get all auths.
*
* @access public
* @return object
*/
public function getAuths()
{
$auths = clone $this->config->sso;
unset($auths->create);
unset($auths->edit);
return $auths;
}
/**
* Get auth by code.
*
* @param string $code
* @access public
* @return object
*/
public function getAuth($code)
{
return $this->config->sso->$code;
}
/**
* Create auth.
*
* @access public
* @return void
*/
public function createAuth()
{
$auth = fixer::input('post')->get();
$items = new stdClass();
$items->{$this->post->code} = $auth;
$this->loadModel('setting')->setItems("system.sso", $items);
}
/**
* Update auth.
*
* @param int $code
* @access public
* @return void
*/
public function updateAuth($code)
{
$auth = fixer::input('post')->get();
$items = new stdClass();
$items->$code = $auth;
$this->loadModel('setting')->setItems("system.sso", $items);
}
/**
* Delete auth.
*
* @param string $code
* @access public
* @return void
*/
public function deleteAuth($code)
{
$this->loadModel('setting')->deleteItems("owner=system&module=sso&section=$code");
}
/**
* Get key of app.
*
* @param string $app
* @access public
* @return object
*/
public function getAppKey($app)
{
return $this->config->sso->$app->key;
}
/**
* Check ip if is allowed.
*
* @param string $app
* @access public
* @return bool
*/
public function checkIP($app)
{
$ipParts = explode('.', $_SERVER['REMOTE_ADDR']);
$allowIPs = explode(',', $this->config->sso->$app->ip);
foreach($allowIPs as $allowIP)
{
$allowIPParts = explode('.', $allowIP);
foreach($allowIPParts as $key => $allowIPPart)
{
if($allowIPPart == '*') $allowIPParts[$key] = $ipParts[$key];
}
if(implode('.', $allowIPParts) == $_SERVER['REMOTE_ADDR']) return true;
}
return false;
}
/**
* Identify user.
*
* @param string $app
* @access public
* @return bool | object
*/
public function identify($app)
{
if(!$this->checkIP($app)) return false;
$key = $this->getAppKey($app);
$account = '';
$authcode = '';
if($this->post->account) $account = $this->post->account;
if($this->get->account) $account = $this->get->account;
if($this->post->authcode) $authcode = $this->post->authcode;
if($this->get->authcode) $authcode = $this->get->authcode;
if(!$account or !$authcode or !$key) return false;
$user = $this->dao->select('*')->from(TABLE_USER)
->where('account')->eq($account)
->andWhere('deleted')->eq(0)
->fetch();
if($user)
{
$code = md5($user->password . $key);
if($code == $authcode) return $user;
}
return false;
}
/**
* Create a key.
*
* @access public
* @return string
*/
public function createKey()
{
return md5(rand());
}
/**
* Get all departments.
*
* @access public
* @return object
*/
public function getAllDepts()
{
return $this->dao->select('*')->from(TABLE_DEPT)->fetchAll();
}
/**
* Get all users.
*
* @access public
* @return object
*/
public function getAllUsers()
{
return $this->dao->select('*')->from(TABLE_USER)
->where('deleted')->eq(0)
->fetchAll();
}
}