http://www.dremi.info/tutorials/php/membuat-manajemen- hak-akses-user-dengan-codeigniter.html Membuat Manajemen Hak Akses User dengan Codeigniter November 6th, 2010 by dr.emi malem eni gw akhirnya bisa juga nyolong2 waktu bwat nulis lagi. setelah skian lama pakum. ada tersirat, panggilan jiwa bwat nulis dan sharing lagi. setelah sekian lama males dan sibuk mncari napkah biar dapur ngebul terus. tapi setidaknya gw masih punya keinginan bwat sharing. pgimanepun, gw terlahir dari kalangan berpendidikan, orang tua gw guru smue. gw masih merasa terpanggil bwat sharing dan berbagi ilmu. tanpa banyak bacot, dan masih dalam bahasa gw yang acakadut ini, kite mulai dah tutorial pertama di bulan nopember ini. haahah!! LEGEK PISAN!! kali ini gw mau sharing tentang codeigniter. yey! di dremi.NET codeigniter kali ini menjadi primadona dalam projek projekna. beberapa taon lalu, gw gak sreg banged kalok make framework orang, tapi berbekal elmu dan inspirasi dari mang opik, hasilnya mampu membius gw bwat make ni framework di segala medan tempur yang berbau web based (PHP). cekakakak!! ni tutorial bakalan ngebahas tentang pgimane mbikin user access bertingkat pada user manager di admin CMS sebagai simulasina. kalok biasana lu pada bikin CMS masih makek 1 tipe user, ada baikna lu pakein dah mulai sekarang. sbtulna script2 ini udah lama gw tanem dalam CMS, tapi beberapa client terkadang meminta bwat menghilangkan nya, yap gw sadar tidak smua client yang mau menggunakannya, mungkin karena keterbatasan nya dalam mempelajari cara penggunaannya. gw pleksibel aja, pgimane permintaan clientna dalam make user manager. Langkah #1 Nyiepin pael pael penting dan memahami konsepnya
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
$config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'x'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; // experimental not currently in use /*END OF PENTING BANGED*/
Update: satu fungsi ini bakalan menghandle form user. Trigger form nya adalah uri segment ke –
3, dengan ketentuan, kalok input user baru maka uri segment 3 kosong, sdangkan kalok mau
update user dengan tujuan mengedit record, uri segment ke – 3 ini akan dikasi value berupa id
record yang bersangkutan.
pada template_helper.php terdapat fungsi get_table_fld(), make_array_key(), post2data(),
store_data(), dan delete_data()
fungsi fungsi tersebut bakalan dibutin di cotroller admin ini, bwat mempermudah aja berok!!
get_table_fld() => mengambil nama nama colom pada table di database
make_array_key() => menyajikan hasil keluaran fungsi get_table_fld() ke dalam array
post2data() => mengkonvert inputan post form ke dalam data yang akan dibutuhkan saat fungsi
store_data() dipanggil
store_data() => menyimpan data kedalam table di database, dimana sesuai trigger form yang
didapat yakni jika inputan post id / sesuai key dalam table bernilai kosong, maka data akan di
insert ke dalam table. sedangkan jika tidak bernilai kosong, maka record bersangkutan akan
diupdate pada tablenya.
delete_data() => menghapus data record bersangkutan
setelah komentar /*MODULE ACCESS*/ pada controller dibawah ini, gw mengeksekusi
beberapa baris perintah. diantaranya bwat memvalidasi jika form digunakan sebagai update data
bersangkutan / input data baru.
semua action dalam module access ini, pada akhirnya akan menghapus record akses user
sebelumnya. jika form digunakan sebagai update data bersangkutan, maka user id yang akan
mengakses module diambil dari post data id user, sdangkan jika form digunakan sebagai input
data baru, maka user id yang akan mengakses module diambil dari user id terakhir yang diinsert
pada table user.
1
2
3
4
5
6
7
8
9
10
11
12
13
/*MODULE ACCESS*/ if($this->input->post('id') != '') { $lastUserID = $this->input->post('id'); } else { //get last id of user $sqlLastUserID = $_this->db->query("SELECT * FROM user ORDER BY id DESC LIMIT 1"); $rowUserID = $sqlLastUserID->row(); $lastUserID = $rowUserID->id; } delete_data('user_access','user_id',$lastUserID);
berikut ini baris bwat menghandle data array dari checkbox yang bernilai module id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$i=0; foreach($this->input->post('module_id') as $mod_id) { $i++; //echo $mod_id; //let's check record of access area user. if record == 0, we will insert
new access, and if >= 1 let's delete the old record before then replace it
with new isert record //it's will protect user to have double or more than one access in same
module
$sqlCheck = $this->db->query("SELECT * FROM user_access WHERE user_id = '".$lastUserID."' AND access_id = '".$mod_id."'"); if($sqlCheck->num_rows() == 0) { $this->db->query("INSERT INTO user_access(user_id, access_id)
VALUES('".$lastUserID."', '".$mod_id."')"); } else { delete_data('user_access','user_id',$lastUserID); $this->db->query("INSERT INTO user_access(user_id, access_id)
VALUES('".$lastUserID."', '".$mod_id."')"); } }
Sedangkan bwat proses pergantian tipe user ke super administrator, kite perlu ngapus data
aksesnya di table user_access, karena kita udah gak perlu lagi ngecek hak akses nya di table
/*Let's process access area module here'*/ if($this->input->post('module_id') != '') { /*MODULE ACCESS*/ if($this->input->post('id') != '') { $lastUserID = $this->input->post('id'); } else { //get last id of user $sqlLastUserID = $_this->db->query("SELECT * FROM user ORDER BY id DESC LIMIT 1"); $rowUserID = $sqlLastUserID->row(); $lastUserID = $rowUserID->id; } delete_data('user_access','user_id',$lastUserID);
$i=0; foreach($this->input->post('module_id') as $mod_id) { $i++; //echo $mod_id; //let's check record of access area user. if record == 0, we will insert
new access, and if >= 1 let's delete the old record before then replace it
with new isert record //it's will protect user to have double or more than one access in same
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module $sqlCheck = $this->db->query("SELECT * FROM user_access WHERE user_id = '".$lastUserID."' AND access_id = '".$mod_id."'"); if($sqlCheck->num_rows() == 0) { $this->db->query("INSERT INTO user_access(user_id, access_id)
VALUES('".$lastUserID."', '".$mod_id."')"); } else { delete_data('user_access','user_id',$lastUserID); $this->db->query("INSERT INTO user_access(user_id, access_id)
VALUES('".$lastUserID."', '".$mod_id."')"); } } } ##remove access rule to set all access trigger (set user as super administrator)## if($this->input->post('user_type') == 1) { delete_data('user_access','user_id',$this->input->post('id')); } /*end of access area module*/ $this->session->set_flashdata('message','Data has been Updated/Saved
Successfull!'); redirect("admin",301); }
function delete() { delete_data('user','id',$this->uri->segment(3)); $this->session->set_flashdata('message','Data has been Deleted
Successfull!'); redirect("admin",301); }
/*Let's create sample of access area'*/ function banner() { $this->load->view('dummy'); }
function filemanager() { $this->load->view('dummy'); } function gallery() { $this->load->view('dummy'); }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function posting() { $this->load->view('dummy'); } } /* End of file welcome.php */ /* Location: ./system/application/controllers/welcome.php */
<label>Password <span class="small">Biarkan kosong jika tidak ingin dirubah</span> </label> <input type="password" name="password" id="password" class="input" />
<div id="stylized" class="myform"> Helloo.... you are in < ?php echo $this->uri->segment(2); ?> area. So, you have access to do something here. </div> </body> </html>
SEDIKIT PENJELASAN PADA FUNGSI accessTrigger() di file template_helper.php
1
2
3
4
//let's skip general access area to checked' $skipped_area = array('','update','delete','save','access_area.php'); if($sql->num_rows() == 1 || in_array($_this->uri->segment(2),