BackUser.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. namespace Cas\Controller\Admin;
  3. use KIF\Core\Request;
  4. use KIF\Verify;
  5. use KIF\Math\Math;
  6. use Cas\Dao\BackUser as DBackUser;
  7. use Cas\Module\BackPassport;
  8. use Cas\Dao\LotteryEvents;
  9. use KIF\Core\Config;
  10. use Cas\Module\Permission;
  11. use Cas\Dao\Platform;
  12. /**
  13. *
  14. * 后台用户管理
  15. * @author lishumingoo@gmail.com
  16. */
  17. class BackUser extends \KIF\Core\BKController {
  18. public function __construct() {
  19. # 是否管理员
  20. $IS_ADMIN = false;
  21. if (\KIF\Core\PermissionController::isSuperadmin()) {
  22. $IS_ADMIN = true;
  23. }
  24. $this->setOutput('IS_ADMIN', $IS_ADMIN);
  25. $this->setOutput('backuser', $this->getUser());
  26. }
  27. public function doLogin() {
  28. if (Request::isPost()) {
  29. $username = Request::p('username');
  30. if (!$username) {
  31. self::ajax_fail_exit('请填写用户姓名');
  32. }
  33. $password = Request::p('password');
  34. if (!$password) {
  35. self::ajax_fail_exit('请填写登陆密码');
  36. }
  37. $app_id = Config::getInstance()->get('App_Id');
  38. $project = Math::md5_16($app_id);
  39. $objBackPassport = new BackPassport();
  40. $tmpResult = $objBackPassport->login($username, $password, $project);
  41. if (!$tmpResult->isSuccess()) {
  42. self::ajax_fail_exit($tmpResult->getData());
  43. }
  44. self::ajax_success_exit();
  45. }
  46. $this->tpl = 'admin/backUser/login';
  47. $title = '登录 - 后台管理工作平台';
  48. $this->setOutput('title', $title);
  49. $this->setOutput('op', Request::g('op'));
  50. }
  51. public function doLogout() {
  52. $objBackPassport = new BackPassport();
  53. $objBackPassport->logout();
  54. self::redirect(Request::schemeDomain() . '/?c=admin_backUser&a=login&op=logout');
  55. }
  56. /**
  57. * 创建新帐号
  58. */
  59. public function doCreateUser() {
  60. \KIF\Core\PermissionController::requireCompetence();
  61. $objDBackUser = new DBackUser();
  62. if (Request::isPost()) {
  63. $errMsg = array('ok' => true);
  64. do {
  65. $name = Request::p('name');
  66. if (!$name) {
  67. $errMsg['msg'] = '请填写姓名';
  68. break;
  69. }
  70. if ($objDBackUser->getIdByName($name)) {
  71. $errMsg['msg'] = "用户名\"{$name}\"已经存在";
  72. break;
  73. }
  74. $platfrom = $_POST['platfrom'];
  75. if (!$platfrom) {
  76. $errMsg['msg'] = "请选择平台列表";
  77. break;
  78. }
  79. $auto_generate = Request::p('auto_generate');
  80. $password = Request::p('password');
  81. if (!$auto_generate && !$password) {
  82. $auto_generate = true;
  83. }
  84. if ($auto_generate) {
  85. $password = crypt(Math::md5_16(time()), 'k');
  86. $_POST['password'] = $password;
  87. }
  88. $email = Request::p('email');
  89. $division = Request::p('division');
  90. $app_id = Config::getInstance()->get('App_Id');
  91. $project = Math::md5_16($app_id);
  92. $info = array(
  93. 'name' => $name,
  94. 'password' => $password,
  95. 'email' => $email,
  96. 'division' => $division,
  97. 'project' => $project,
  98. 'app_id' => $app_id,
  99. 'permission'=> 'ordinary',
  100. 'platfrom' => $platfrom,
  101. );
  102. $tmpResult = $objDBackUser->add($info);
  103. if (!$tmpResult->isSuccess()) {
  104. $errMsg['msg'] = $tmpResult->getData();
  105. } else {
  106. $user = $tmpResult->getData();
  107. $user['password'] = $password;
  108. $uid = $user['uid'];
  109. # 建立权限组
  110. // $objDKifUsergroup = new \Cas\Dao\KifUsergroup();
  111. // $groupid = $objDKifUsergroup->add(array('description' => $name));
  112. // $objDKifUsergroupRelation = new \Cas\Dao\KifUsergroupRelation();
  113. // $objDKifUsergroupRelation->add(array('uid' => $uid, 'groupid' => $groupid));
  114. // $objDBackUser->modify(array('groupid' => $groupid), array('uid' => $uid));
  115. }
  116. } while (false);
  117. if ($errMsg['msg']) {
  118. $errMsg['ok'] = false;
  119. }
  120. $this->setOutput('submitData', $_POST);
  121. $this->setOutput('errMsg', $errMsg);
  122. $this->setOutput('new_user', $user);
  123. }
  124. $app_id = Config::getInstance()->get('App_Id');
  125. $project = Math::md5_16($app_id);
  126. $uids = $objDBackUser->findIdsBy(array('project' => $project));
  127. $users = $objDBackUser->gets($uids);
  128. krsort($users);
  129. $objDPlatfrom = new Platform ();
  130. $platfrom = $objDPlatfrom->getsAll('id desc');
  131. $this->tpl = 'admin/backUser/user_list';
  132. $title = '帐号管理';
  133. $this->setOutput('title', $title);
  134. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  135. $this->addNavMenu('平台设置');
  136. $this->addNavMenu($title);
  137. $this->setOutput('users', $users);
  138. $this->setOutput('platfrom', $platfrom);
  139. }
  140. public function doDeleteUser() {
  141. \KIF\Core\PermissionController::requireCompetence();
  142. $uid = Request::p('uid');
  143. if (!Verify::unsignedInt($uid)) {
  144. $this->ajax_fail_exit('无效用户id');
  145. }
  146. $objDBackUser = new DBackUser();
  147. $tmpResult = $objDBackUser->delete(array('uid'=>$uid));
  148. $this->ajax_success_exit();
  149. }
  150. /**
  151. * 后台用户列表
  152. */
  153. public function doUserList() {
  154. \KIF\Core\PermissionController::requireCompetence();
  155. $objDBackUser = new DBackUser();
  156. $app_id = Config::getInstance()->get('App_Id');
  157. $project = Math::md5_16($app_id);
  158. $condition = "project = '{$project}' || permission = 'admin'";
  159. $uids = $objDBackUser->findIdsBy($condition);
  160. $users = $objDBackUser->gets($uids);
  161. krsort($users);
  162. $objDPlatfrom = new Platform ();
  163. $platfrom = $objDPlatfrom->getsAll('id desc');
  164. $this->tpl = 'admin/backUser/user_list';
  165. $title = '帐号管理';
  166. $this->setOutput('title', $title);
  167. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  168. $this->addNavMenu('平台设置');
  169. $this->addNavMenu($title);
  170. $this->setOutput('users', $users);
  171. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  172. $this->setOutput('platfrom', $platfrom);
  173. }
  174. /**
  175. * 修改密码 - 自助
  176. */
  177. public function doModifyPassword() {
  178. \KIF\Core\PermissionController::requireCompetence();
  179. $uid = Request::g('uid');
  180. if (!Verify::unsignedInt($uid)) {
  181. self::fail_exit_bs('无效uid');
  182. }
  183. $objDBackUser = new DBackUser();
  184. $user = $objDBackUser->get($uid);
  185. if (!$user) {
  186. self::fail_exit_bs('用户不存在');
  187. }
  188. if (Request::isPost()) {
  189. do {
  190. $oldPassword = Request::p('oldPassword');
  191. if (!$oldPassword) {
  192. $errMsg = '原始密码为空';
  193. break;
  194. }
  195. $newPassword = Request::p('newPassword');
  196. if (!$newPassword) {
  197. $errMsg = '新密码为空';
  198. break;
  199. }
  200. $tmpResult = $objDBackUser->modifyPassword($uid, $oldPassword, $newPassword);
  201. if (!$tmpResult->isSuccess()) {
  202. $errMsg = $tmpResult->getData();
  203. break;
  204. }
  205. $successMsg = '密码修改成功';
  206. } while (false);
  207. }
  208. $this->tpl = 'admin/backUser/modify_password';
  209. $title = '密码修改';
  210. $this->setOutput('title', $title);
  211. $this->setOutput('menu_active', array('name' => 'modifypassword', 'item' => '')); //激活菜单
  212. $this->addNavMenu('帐号管理');
  213. $this->addNavMenu($title);
  214. $this->setOutput('errMsg', $errMsg);
  215. $this->setOutput('successMsg', $successMsg);
  216. $this->setOutput('user', $user);
  217. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  218. }
  219. /**
  220. * 密码修改 - 超级管理员使用
  221. */
  222. public function doMP() {
  223. \KIF\Core\PermissionController::requireCompetence();
  224. $uid = Request::g('uid');
  225. if (!Verify::unsignedInt($uid)) {
  226. self::fail_exit_bs('无效uid');
  227. }
  228. $objDBackUser = new DBackUser();
  229. $user = $objDBackUser->get($uid);
  230. if (!$user) {
  231. self::fail_exit_bs('用户不存在');
  232. }
  233. if (Request::isPost()) {
  234. do {
  235. $password = Request::p('password');
  236. $auto_generate = Request::p('auto_generate');
  237. if (!$password && $auto_generate) {
  238. $password = crypt(Math::md5_16(time()), '@w');
  239. } else {
  240. if (strlen($password) < 6) {
  241. $errMsg['ok'] = 1;
  242. $errMsg['msg'] = '密码不能小于8位';
  243. break;
  244. }
  245. }
  246. $tableInfo = array(
  247. 'password' => Math::md5_16($password),
  248. );
  249. $condition = array(
  250. 'uid' => $uid,
  251. );
  252. $tmpResult = $objDBackUser->update($tableInfo, $condition);
  253. if (!$tmpResult) {
  254. $errMsg['ok'] = 1;
  255. $errMsg['msg'] = '密码修改失败';
  256. }
  257. $errMsg['ok'] = 2;
  258. $errMsg['msg'] = "密码修改成功。新密码: {$password}";
  259. } while (false);
  260. }
  261. $this->tpl = 'admin/backUser/mp';
  262. $title = '密码修改';
  263. $this->setOutput('title', $title);
  264. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  265. $this->addNavMenu('用户管理');
  266. $this->addNavMenu($title);
  267. $this->setOutput('user', $user);
  268. $this->setOutput('password', $password);
  269. $this->setOutput('errMsg', $errMsg);
  270. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  271. }
  272. /**
  273. * 权限设置
  274. * 选择角色:管理员、普通账号
  275. */
  276. public function doSetPermission() {
  277. \KIF\Core\PermissionController::requireCompetence();
  278. $uid = Request::g('uid');
  279. if (!Verify::unsignedInt($uid)) {
  280. self::fail_exit_bs('无效uid');
  281. }
  282. $objDBackUser = new DBackUser();
  283. $user = $objDBackUser->get($uid);
  284. if (!$user) {
  285. self::fail_exit_bs('用户不存在');
  286. }
  287. if (Request::isPost()) {
  288. $permission = Request::p('permission');
  289. if (!in_array($permission, array('admin', 'ordinary'))) {
  290. $this->ajax_fail_exit('不存在的角色');
  291. }
  292. $info = array('permission' => $permission);
  293. $condition = array('uid' => $uid);
  294. $objDBackUser->modify($info, $condition);
  295. $user['permission'] = $permission;
  296. $errMsg['ok'] = true;
  297. }
  298. $this->tpl = 'admin/backUser/permission';
  299. $title = '权限设置';
  300. $this->setOutput('title', $title);
  301. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  302. $this->addNavMenu('用户管理');
  303. $this->addNavMenu($title);
  304. $this->setOutput('user', $user);
  305. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  306. $this->setOutput('errMsg', $errMsg);
  307. }
  308. public function doSetPlatfrom() {
  309. \KIF\Core\PermissionController::requireCompetence();
  310. $uid = Request::g('uid');
  311. if (!Verify::unsignedInt($uid)) {
  312. self::fail_exit_bs('无效uid');
  313. }
  314. $objDBackUser = new DBackUser();
  315. $user = $objDBackUser->get($uid);
  316. if (!$user) {
  317. self::fail_exit_bs('用户不存在');
  318. }
  319. if (Request::isPost()) {
  320. $platfrom = $_POST['platfrom'];
  321. if (!$platfrom) {
  322. $this->ajax_fail_exit('不存在的角色');
  323. }
  324. $info = array('platfrom' => $platfrom);
  325. $condition = array('uid' => $uid);
  326. $objDBackUser->modify($info, $condition);
  327. $user['platfrom'] = $platfrom;
  328. $errMsg['ok'] = true;
  329. }
  330. $objDPlatfrom = new Platform ();
  331. $platfrom = $objDPlatfrom->getsAll('id desc');
  332. $this->tpl = 'admin/backUser/platfrom';
  333. $title = '权限设置';
  334. $this->setOutput('title', $title);
  335. $this->setOutput('menu_active', array('name' => 'userslist', 'item' => '')); //激活菜单
  336. $this->addNavMenu('用户管理');
  337. $this->addNavMenu($title);
  338. $this->setOutput('user', $user);
  339. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  340. $this->setOutput('errMsg', $errMsg);
  341. $this->setOutput('platfrom', $platfrom);
  342. }
  343. public function getPagePublicData() {
  344. $adminPublicArray = array ();
  345. $adminPublicArray ['rapidEntranceUrl'] = $this->getRapidEntrance (); // 头部导航"新建"数据
  346. return $adminPublicArray;
  347. }
  348. /**
  349. * 头部导航"新建"数据
  350. */
  351. public function getRapidEntrance() {
  352. $url = Request::schemeDomain () . '/?c=Admin_Activity_SetBasics&a=PageCreate&type=';
  353. $fastCreateActivityUrlData = array (
  354. '大转盘' => $url . LotteryEvents::TYPE_EVENTS_TURNTABLE . '&bigType=Event',
  355. '刮刮卡' => $url . LotteryEvents::TYPE_EVENTS_SCRATCH . '&bigType=Event',
  356. '邀请函' => $url . LotteryEvents::TYPE_EVENTS_INVITATION . '&bigType=Event',
  357. '优惠券' => $url . LotteryEvents::TYPE_EVENTS_CODE . '&bigType=Event',
  358. '试用' => $url . LotteryEvents::TYPE_EVENTS_TRY . '&bigType=Event',
  359. '问卷调查' => $url . LotteryEvents::TYPE_EVENTS_SURVEY . '&bigType=Event',
  360. '投票' => $url . LotteryEvents::TYPE_EVENTS_VOTE . '&bigType=Event',
  361. '切屏专题' => $url . LotteryEvents::TYPE_EVENTS_CUT_SCREEN . '&bigType=H5',
  362. '文章' => $url . LotteryEvents::TYPE_EVENTS_ARTICLE . '&bigType=Article',
  363. '其他' => $url . LotteryEvents::TYPE_EVENTS_OTHER . '&bigType=Event',
  364. );
  365. return $fastCreateActivityUrlData;
  366. }
  367. public function display() {
  368. return $this->render();
  369. }
  370. }