From 2feb0e29926034e0694d67efb668a2f29d820fdf Mon Sep 17 00:00:00 2001 From: Daniel Camargo Date: Wed, 30 Oct 2013 15:54:04 -0200 Subject: [PATCH] partial #1964 - option to create admin users --- .../default/cockpit/giftcards/index.php | 1 + .../default/cockpit/permissions/index.php | 11 + .../default/cockpit/permissions/users.php | 64 ++++++ .../crunchbutton/api/permissions/users.php | 68 ++++++ include/library/Crunchbutton/Admin.php | 53 +++++ include/library/Crunchbutton/Admin/Group.php | 11 + include/library/Crunchbutton/Group.php | 11 + .../cockpit/giftcards/groups/index.phtml | 1 + .../views/default/cockpit/layout/core.phtml | 2 +- .../default/cockpit/permissions/index.phtml | 12 + .../cockpit/permissions/users/content.phtml | 33 +++ .../cockpit/permissions/users/form.phtml | 210 ++++++++++++++++++ .../cockpit/permissions/users/index.phtml | 68 ++++++ www/assets/js/admin.js | 31 +++ 14 files changed, 575 insertions(+), 1 deletion(-) create mode 100644 include/controllers/default/cockpit/permissions/index.php create mode 100644 include/controllers/default/cockpit/permissions/users.php create mode 100644 include/controllers/default/crunchbutton/api/permissions/users.php create mode 100644 include/library/Crunchbutton/Admin/Group.php create mode 100644 include/library/Crunchbutton/Group.php create mode 100644 include/views/default/cockpit/permissions/index.phtml create mode 100644 include/views/default/cockpit/permissions/users/content.phtml create mode 100644 include/views/default/cockpit/permissions/users/form.phtml create mode 100644 include/views/default/cockpit/permissions/users/index.phtml diff --git a/include/controllers/default/cockpit/giftcards/index.php b/include/controllers/default/cockpit/giftcards/index.php index 4866bc8bd..b4e84438a 100644 --- a/include/controllers/default/cockpit/giftcards/index.php +++ b/include/controllers/default/cockpit/giftcards/index.php @@ -1,6 +1,7 @@ permission()->check(['global', 'gift-card-all', 'gift-card-list-page', 'gift-card-list-all'])) { diff --git a/include/controllers/default/cockpit/permissions/index.php b/include/controllers/default/cockpit/permissions/index.php new file mode 100644 index 000000000..cd7ee843d --- /dev/null +++ b/include/controllers/default/cockpit/permissions/index.php @@ -0,0 +1,11 @@ +display('permissions/index'); + + } + +} \ No newline at end of file diff --git a/include/controllers/default/cockpit/permissions/users.php b/include/controllers/default/cockpit/permissions/users.php new file mode 100644 index 000000000..7e6b83edc --- /dev/null +++ b/include/controllers/default/cockpit/permissions/users.php @@ -0,0 +1,64 @@ +permission()->check(['global','permissions-all', 'permission-users'])) { + return ; + } + + $action = c::getPagePiece(2); + + switch ( $action ) { + + case 'content': + $this->search(); + break; + + case 'new': + $this->form(); + break; + + case 'remove': + $id_admin = $_REQUEST[ 'id_admin' ]; + $admin = Crunchbutton_Admin::o( $id_admin ); + if( $admin->id_admin ){ + $admin->delete(); + } + echo 'ok'; + break; + + default: + if( is_numeric( $action ) ){ + $this->form(); + exit; + } + c::view()->page = 'permissions'; + c::view()->display('permissions/users/index'); + break; + } + + } + + private function search(){ + $search = []; + if ( $_REQUEST[ 'name' ] ) { + $search[ 'name' ] = $_REQUEST[ 'name' ]; + } + c::view()->admins = Crunchbutton_Admin::find( $search ); + c::view()->layout( 'layout/ajax' ); + c::view()->display( 'permissions/users/content' ); + } + + private function form(){ + $id_admin = c::getPagePiece(2); + if( $id_admin != 'new' ){ + c::view()->admin = Crunchbutton_Admin::o( $id_admin ); + } else { + c::view()->admin = new Crunchbutton_Admin(); + } + c::view()->display( 'permissions/users/form' ); + } + +} \ No newline at end of file diff --git a/include/controllers/default/crunchbutton/api/permissions/users.php b/include/controllers/default/crunchbutton/api/permissions/users.php new file mode 100644 index 000000000..99d011e20 --- /dev/null +++ b/include/controllers/default/crunchbutton/api/permissions/users.php @@ -0,0 +1,68 @@ +permission()->check(['global','permissions-all', 'permission-users'])) { + return ; + } + + switch ( $this->method() ) { + + case 'post': + + $id_admin = c::getPagePiece( 3 ); + + if( !$id_admin ){ + if( Crunchbutton_Admin::loginExists( $_REQUEST[ 'login' ] ) ){ + echo json_encode( [ 'error' => 'login' ] ); + exit(); + } + } + + $name = $_REQUEST[ 'name' ]; + $phone = $_REQUEST[ 'phone' ]; + $txt = $_REQUEST[ 'txt' ]; + $email = $_REQUEST[ 'email' ]; + $testphone = $_REQUEST[ 'testphone' ]; + $timezone = $_REQUEST[ 'timezone' ]; + $login = $_REQUEST[ 'login' ]; + $password = $_REQUEST[ 'password' ]; + $ids_group = $_REQUEST[ 'id_group' ]; + if( $id_admin ){ + $admin = Crunchbutton_Admin::o( $id_admin ); + } else { + $admin = new Crunchbutton_Admin(); + } + $admin->name = $name; + $admin->phone = $phone; + $admin->txt = $txt; + $admin->email = $email; + $admin->testphone = $testphone; + $admin->timezone = $timezone; + $admin->login = $login; + if( $password != '' ){ + $admin->password = $password; + } + $admin->save(); + $admin->removeGroups(); + $ids_group = explode( ',' , $ids_group ); + if( $ids_group ){ + foreach ( $ids_group as $id_group ) { + $new = new Crunchbutton_Admin_Group(); + $new->id_admin = $admin->id_admin; + $new->id_group = intval( $id_group ); + $new->save(); + } + } + echo json_encode( ['success' => $admin->id_admin ] ); + break; + default: + echo json_encode( [ 'error' => 'invalid object' ] ); + break; + } + } + + +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Admin.php b/include/library/Crunchbutton/Admin.php index af31896ca..08e532932 100644 --- a/include/library/Crunchbutton/Admin.php +++ b/include/library/Crunchbutton/Admin.php @@ -76,11 +76,64 @@ class Crunchbutton_Admin extends Cana_Table { return $this->_communities; } + + public function loginExists( $login ){ + if( trim( $login ) != '' ){ + return Crunchbutton_Admin::login( $login ); + } + return false; + } + + public function groups(){ + if( !$this->_groups ){ + $this->_groups = Crunchbutton_Group::q( "SELECT g.* FROM `group` g INNER JOIN admin_group ag ON ag.id_group = g.id_group AND ag.id_admin = {$this->id_admin} ORDER BY name ASC" ); + } + return $this->_groups; + } + + public function removeGroups(){ + Cana::db()->query( "DELETE FROM `admin_group` WHERE id_admin = {$this->id_admin}" ); + } + + public function hasGroup( $id_group ){ + $groups = $this->groups(); + foreach( $groups as $group ){ + if( $id_group == $group->id_group ){ + return true; + } + } + return false; + } + + public function groups2str(){ + $groups = $this->groups(); + $str = ''; + $commas = ''; + foreach( $groups as $group ){ + $str .= $commas . $group->name; + $commas = ', '; + } + return $str; + } public function makePass($pass) { return sha1(c::crypt()->encrypt($pass)); } + public static function find($search = []) { + + $query = 'SELECT `admin`.* FROM `admin` WHERE id_admin IS NOT NULL '; + + if ( $search[ 'name' ] ) { + $query .= " AND name LIKE '%{$search[ 'name' ]}%' "; + } + + $query .= " ORDER BY name DESC"; + + $gifts = self::q($query); + return $gifts; + } + public function __construct($id = null) { parent::__construct(); $this diff --git a/include/library/Crunchbutton/Admin/Group.php b/include/library/Crunchbutton/Admin/Group.php new file mode 100644 index 000000000..c40942059 --- /dev/null +++ b/include/library/Crunchbutton/Admin/Group.php @@ -0,0 +1,11 @@ +table('admin_group') + ->idVar('id_admin_group') + ->load($id); + } +} \ No newline at end of file diff --git a/include/library/Crunchbutton/Group.php b/include/library/Crunchbutton/Group.php new file mode 100644 index 000000000..65b5c4482 --- /dev/null +++ b/include/library/Crunchbutton/Group.php @@ -0,0 +1,11 @@ +table('group') + ->idVar('id_group') + ->load($id); + } +} \ No newline at end of file diff --git a/include/views/default/cockpit/giftcards/groups/index.phtml b/include/views/default/cockpit/giftcards/groups/index.phtml index 63f6759f1..19b8495c2 100644 --- a/include/views/default/cockpit/giftcards/groups/index.phtml +++ b/include/views/default/cockpit/giftcards/groups/index.phtml @@ -41,6 +41,7 @@ \ No newline at end of file diff --git a/include/views/default/cockpit/permissions/users/index.phtml b/include/views/default/cockpit/permissions/users/index.phtml new file mode 100644 index 000000000..ab3b1dc20 --- /dev/null +++ b/include/views/default/cockpit/permissions/users/index.phtml @@ -0,0 +1,68 @@ +title = 'Permissions'; + $this->titleicon = 'lock'; + $this->titleLink = '/permissions'; + + $this->title2 = 'Users'; + $this->title2icon = 'user'; + +?> + +
+
+
+
+
+
+
    +
  • + +
  • +
+
+
+ +
+
+
+
+
+
Results
+
+
+
+
+
+
+
+
+
+ \ No newline at end of file diff --git a/www/assets/js/admin.js b/www/assets/js/admin.js index 37766dfa6..4e050795b 100644 --- a/www/assets/js/admin.js +++ b/www/assets/js/admin.js @@ -1809,3 +1809,34 @@ App.giftcardsGroup = { } +App.permissions = {}; +App.permissions.admin = { + params: function() { + return { + name: $('input[name="name"]').val() + }; + }, + load: function() { + $('.permissions-loader').show(); + $('.permissions-content').html(''); + $.ajax({ + url: '/permissions/users/content', + data: App.permissions.admin.params(), + complete: function(content) { + $('.permissions-content').html(content.responseText); + $('.permissions-loader').hide(); + } + }); + }, + remove: function( id_admin ){ + $.ajax({ + url: '/permissions/users/remove', + type: "POST", + data: { 'id_admin': id_admin } , + complete: function() { + App.permissions.admin.load(); + } + }); + }, +} +