728x90
반응형
1. 저장 프로시저 (getAdmin) 생성
$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
DELIMITER $$
CREATE PROCEDURE getAdmin(i_sno INT)
BEGIN
SELECT sno, userid, password, nick FROM admins WHERE sno = i_sno LIMIT 1;
END $$
DELIMITER ;
mysql> CALL getAdmin(2);
+------+---------+----------+-------+
| sno | userid | password | nick |
+------+---------+----------+-------+
| 2 | testid2 | passwd2 | nick2 |
+------+---------+----------+-------+
1 ROW IN SET (0.00 sec)
Query OK, 0 ROWS affected (0.00 sec)
mysql> exit
2. 뷰 디렉토리에 xyz/resources/views/mgmt/admin/chg_passwd_form.blade.html 파일을 생성한다.
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">관리자 비밀번호 변경</h4>
</div>
<div class="modal-body">
<form name="chg_passwd_form" action="/mgmt/admin/chg_passwd" method="post">
@csrf
<div class="form-group">
<label>아이디</label>
<input type="text" name="userid" class="form-control" readonly required value="{{ $admin->userid }}"/>
<input type="hidden" name="id" value="{{ $admin->sno }}" />
</div>
<div class="form-group">
<label>비밀번호 <small>(필수)</small></label>
<input type="password" name="passwd1" class="form-control" required />
</div>
<div class="form-group">
<label>비밀번호 확인 <small>(필수)</small></label>
<input type="password" name="passwd2" class="form-control" required />
</div>
<div class="form-group" style="text-align: right">
<input class="btn btn-primary" type="submit" value="관리자 비밀번호 변경" />
</div>
</form>
</div>
3. 컨트롤러(xyz/app/Http/Controllers/Mgmt/Admin.php)에 다음을 추가한다.
...
// 관리자 비밀번호변경 폼
// /mgnt/admin/chg_passwd_form/{id}
public function chg_passwd_form ($id) {
$t_admin = DB::select('CALL getAdmin(?)', [$id]);
$admin = $t_admin[0];
// $admin = collect(DB::select('CALL getAdmin(?)', [$id]))->first();
return view('mgmt.admin.chg_passwd_form', ['admin' => $admin]);
}
...
4. 라우터(xyz/routes/web.php)에 다음을 추가한다.
Route::get('admin/chg_passwd_form/{id}', [Admin::class, 'chg_passwd_form']);
5. 이제, 비밀번호변경 버튼을 클릭하면 비밀번호변경을 위한 모달 다이얼로그박스가 나타날 것이다.
6. 실제로 비밀번호를 변경하는 작업을 해보자. 우선 저장 프로시저 (updateAdminPassword) 생성하자.
$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> DELIMITER $$
mysql> CREATE PROCEDURE updateAdminPassword
-> (i_sno INT,
-> i_password VARCHAR(255))
-> BEGIN
-> UPDATE admins SET password = i_password WHERE sno = i_sno;
-> END $$
Query OK, 0 ROWS affected (0.01 sec)
mysql> DELIMITER ;
mysql>
mysql> CALL listAdmins();
+-----+---------+----------+-------+
| sno | userid | password | nick |
+-----+---------+----------+-------+
| 1 | testid1 | passwd1 | nick1 |
| 2 | testid2 | passwd2 | nick2 |
| 3 | testid3 | passwd3 | nick3 |
| 4 | testid4 | passwd4 | nick4 |
| 5 | testid5 | passwd5 | nick5 |
| 9 | testid6 | passwd6 | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
Query OK, 0 ROWS affected (0.00 sec)
mysql> CALL updateAdminPassword(1, 'passwd101');
Query OK, 1 ROW affected (0.00 sec)
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid | password | nick |
+-----+---------+-----------+-------+
| 1 | testid1 | passwd101 | nick1 |
| 2 | testid2 | passwd2 | nick2 |
| 3 | testid3 | passwd3 | nick3 |
| 4 | testid4 | passwd4 | nick4 |
| 5 | testid5 | passwd5 | nick5 |
| 9 | testid6 | passwd6 | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
Query OK, 0 ROWS affected (0.00 sec)
mysql> exit
7. 그리고, 컨트롤러(xyz/app/Http/Controllers/Mgmt/Admin.php)에 다음을 추가한다.
// 관리자 비밀번호변경
// /mgmt/admin/chg_passwd
public function chg_passwd (Request $request) {
$id = $request->input('id');
$passwd1 = $request->input('passwd1');
$passwd2 = $request->input('passwd2');
if ($passwd1 != $passwd2) {
return redirect('/mgmt/admin');
}
DB::update('CALL updateAdminPassword(?, ?)', [$id, $passwd1]);
return redirect('/mgmt/admin');
}
8. 라우터(xyz/routes/web.php)에는 다음을 추가한다.
Route::post('admin/chg_passwd', [Admin::class, 'chg_passwd']);
9. 비밀번호를 변경해보고, DB의 내용이 잘 반영되었는지 확인해보자.
$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 241
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid | password | nick |
+-----+---------+-----------+-------+
| 1 | testid1 | passwd101 | nick1 |
| 2 | testid2 | passwd2 | nick2 |
| 3 | testid3 | passwd3 | nick3 |
| 4 | testid4 | passwd4 | nick4 |
| 5 | testid5 | passwd5 | nick5 |
| 9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
728x90
반응형
'Framework (Backend) > Laravel' 카테고리의 다른 글
(Lavavel) 관리자 삭제하기 : Laravel 8.x (0) | 2021.08.29 |
---|---|
(Lavavel) 관리자 수정하기 : Laravel 8.x (0) | 2021.08.29 |
(Lavavel) 관리자 추가하기 : Laravel 8.x (0) | 2021.08.24 |
(Lavavel) 관리자 목록 보여주기 : Laravel 8.x (0) | 2021.08.23 |
(Lavavel) 첫페이지 만들어보기 : Laravel 8.x (0) | 2021.08.23 |