public function runAction() { $em = $this->getEntityManager(); $console = $this->getServiceLocator()->get('console'); $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em); $console->writeLine('建立資料表中, 請稍待!!', ColorInterface::GREEN); $classes = $em->getMetadataFactory()->getAllMetadata(); if ($this->params()->fromRoute('re-create-database')) { $schemaTool->dropSchema($classes); } $schemaTool->createSchema($classes); // 安裝預設管理人員及選單 $username = '******'; $password = \Zend\Math\Rand::getString(8, null, true); $user = new \Base\Entity\User(); $user->setUsername($username); $user->setPassword(\Zend\Ldap\Attribute::createPassword($password)); $user->setDisplayName('管理者'); $user->setRole('admin'); $em->persist($user); $em->flush(); $menu = new \Base\Entity\Menu(); $menu->setName('首頁'); $menu->setUser($user); $params = ['max_records' => 10, 'order_kind' => 'desc', 'term' => '']; $menu->setParams(serialize($params)); $em->persist($menu); $em->flush(); $console->writeLine('建立完成!!', ColorInterface::GREEN); $console->writeLine('預設帳號 ' . $username . ', 密碼 ' . $password, ColorInterface::GREEN); }
public function saveAction() { $jsonModel = new JsonModel(); if ($this->getRequest()->isPost()) { $data = $this->params()->fromPost(); $em = $this->getEntityManager(); if (!($userRes = $em->getRepository('Base\\Entity\\User')->find($data['id']))) { $userRes = new \Base\Entity\User(); } $form = new UserForm(); $roleArr = $em->getRepository('Base\\Entity\\User')->getRoleArray(); $form->get('role')->setValueOptions($roleArr); $form->setData($data); if ($form->isValid()) { $userRes->setDisplayName($data['display_name']); $userRes->setUsername($data['username']); $userRes->setRole($data['role']); $userRes->setPassword(\Zend\Ldap\Attribute::createPassword($data['password'])); $em->persist($userRes); $em->flush(); $jsonModel->setVariable('success', true); } else { $jsonModel->setVariable('success', false); $jsonModel->setVariable('message', $form->getMessages()); } } return $jsonModel; }
public function runAction() { $username = $this->params()->fromRoute('username'); $password = $this->params()->fromRoute('password'); $em = $this->getEntityManager(); $qb = $em->createQueryBuilder()->select('u')->from('Base\\Entity\\User', 'u')->where('u.username=:username')->setParameter('username', $username)->getQuery()->getResult(); if (count($qb)) { $userRes = $qb[0]; } else { $userRes = new \Base\Entity\User(); $userRes->setUsername($username); $userRes->setDisplayName('管理者'); } $userRes->setPassword(\Zend\Ldap\Attribute::createPassword($password)); $userRes->setRole('admin'); $em->persist($userRes); $em->flush(); echo "\n {$username} 設定成功 \n "; }