2015-10-22

beego Tutorial 6 : 관리자 비밀번호 변경하기



  • xyz/views/admin/index.html 에 다음처럼 비밀번호변경 버튼을 넣어준다.
    ...
     
    <th style="text-align: center">아이디</th>
    <th style="text-align: center">별명</th>
    <th style="text-align: center">수정/삭제</th>
     
    ...
     
        <td style="text-align: center">{{$admins.Userid}}</td>
        <td style="text-align: center">{{$admins.Nick}}</td>
        <td style="text-align: center">
          <a href="/admin/chg_passwd_form/{{$admins.Id}}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">비밀번호변경</a>
        </td>
     
    ...
  • 모델(xyz/models/admin.go)에 다음을 추가한다.
    ...
     
    func (this *Admin) Read(id int) {
        o := orm.NewOrm()
        o.Using("default")
        this.Id = id
        o.Read(this)
    }
     
    ...
  • 뷰 디렉토리에 xyz/views/admin/chg_passwd_form.html 파일을 생성한다.
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title">관리자 비밀번호 변경</h4>
    </div>
    <div class="modal-body">
      <form name="chg_passwd_form" action="/admin/chg_passwd" method="post">
        {{ .xsrfdata }}
        <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.Id}}" />
        </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>
  • 컨트롤러(xyz/controllers/admin.go)에 다음을 추가한다. 그리고, strconv 도 임포트해준다.
    ...
     
    // 관리자 비밀번호변경 폼
    func (c *AdminController) Chg_passwd_form() {
        id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
        admin := new(models.Admin)
        admin.Read(id)
     
        c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml())
        c.Data["admin"] = admin
        c.TplNames = "admin/chg_passwd_form.html"
    }
     
    ...
  • 라우터()에 다음을 추가한다.
    beego.Router("/admin/chg_passwd_form/:id", &c.AdminController{}, "get:Chg_passwd_form")
  • 이제, 비밀번호변경 버튼을 클릭하면 비밀번호변경을 위한 모달 다이얼로그박스가 나타날 것이다.
  • 실제로 비밀번호를 변경하는 작업을 해보자. 우선 모델(xyz/models/admin.go)에 다음을 추가한다.
    func (this *Admin) Change_password(id int, passwd string) {
        o := orm.NewOrm()
        o.Using("default")
     
        this.Id = id
        o.Read(this)
        this.Password = passwd
     
        o.Update(this)
    }
  • 그리고, 컨트롤러(xyz/controllers/admin.go)에 다음을 추가한다.
    // 관리자 비밀번호변경
    func (c *AdminController) Chg_passwd() {
        id, _ := c.GetInt("id")
        passwd1 := c.GetString("passwd1")
        passwd2 := c.GetString("passwd2")
     
        if passwd1 != passwd2 {
            c.Redirect(c.UrlFor(".Index"), 302)
            return
        }
     
        admin := new(models.Admin)
        admin.Change_password(id, passwd1)
     
        c.Redirect(c.UrlFor(".Index"), 302)
    }
  • 라우터(xyz/routers/router.go)에는 다음을 추가한다.
    beego.Router("/admin/chg_passwd", &c.AdminController{}, "post:Chg_passwd")
  • 비밀번호를 변경해보고, DB의 내용이 잘 반영되었는지 확인해보자.
    $ sqlite3 xyz.db
    sqlite> select * from tb_admin;
    1|testid1|passwd1|nick1
    2|testid2|passwd2|nick2
    3|testid3|passwd3|nick3
    4|testid4|passwd4|nick4
    5|testid5|passwd5|nick5
    6|admin|newpassword|Admin
    sqlite>

댓글 없음:

댓글 쓰기