From 6954ee9d89a75bf154bf7292beb4b72bf9d35b92 Mon Sep 17 00:00:00 2001 From: xia0ta0 Date: Fri, 19 Jul 2013 14:40:51 +0800 Subject: [PATCH] * code for task#1580. --- module/group/lang/resource.php | 14 +++ module/sso/control.php | 143 ++++++++++++++++++++++++--- module/sso/model.php | 173 +++++++++++++++++++++++++++++++++ 3 files changed, 319 insertions(+), 11 deletions(-) diff --git a/module/group/lang/resource.php b/module/group/lang/resource.php index 5ad0537890..ba39590795 100644 --- a/module/group/lang/resource.php +++ b/module/group/lang/resource.php @@ -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'; diff --git a/module/sso/control.php b/module/sso/control.php index d9d2e69ede..6511fafd64 100644 --- a/module/sso/control.php +++ b/module/sso/control.php @@ -1,21 +1,102 @@ 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); } } diff --git a/module/sso/model.php b/module/sso/model.php index 8cf7c30425..8a867a0a81 100644 --- a/module/sso/model.php +++ b/module/sso/model.php @@ -1,5 +1,178 @@ 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§ion=$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(); + } }