[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug和功能改进 #2

Closed
mikemouse51 opened this issue Feb 15, 2017 · 2 comments
Closed

bug和功能改进 #2

mikemouse51 opened this issue Feb 15, 2017 · 2 comments
Labels

Comments

@mikemouse51
Copy link
mikemouse51 commented Feb 15, 2017

以下是bug,加粗的地方是需要添加的地方:
bug1:

func (c *PermissionController) Save() {
    flash := beego.NewFlash()
    pid, _ := strconv.Atoi(c.Input().Get("pid"))
    name, url, description := c.Input().Get("name"), c.Input().Get("url"), c.Input().Get("description")
    **if pid == 0{
        flash.Error("请添加父节点")
        flash.Store(&c.Controller)
        c.Redirect("/permission/add?pid=", 302)**
    }else if pid > 0 && len(name) == 0 {...}

bug2:

func (c *PermissionController) Update() {
    flash := beego.NewFlash()
    id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
    pid, _ := strconv.Atoi(c.Input().Get("pid"))
    name, url, description := c.Input().Get("name"), c.Input().Get("url"), c.Input().Get("description")
    **if pid == 0{
        flash.Error("请添加父节点")
        flash.Store(&c.Controller)
        c.Redirect("/permission/edit/" + strconv.Itoa(id), 302)**
    }else if pid > 0 && len(name) == 0 {...}

bug3:

func (c *UserController) UpdateAvatar() {
	flash := beego.NewFlash()
	f, h, err := c.GetFile("avatar")
	**if err == http.ErrMissingFile {
		flash.Error("请先上传文件")
		flash.Store(&c.Controller)
		c.Redirect("/user/setting", 302)
	}**
	defer f.Close()
	if err != nil {
		flash.Error("上传失败")
		flash.Store(&c.Controller)
		c.Redirect("/user/setting", 302)
		return

bug4:
模板views/permission/list.tpl

<tbody>
         {{range .Permissions}}
         <tr>
           <td>{{.Id}}</td>
           <td>{{.Name}}</td>
           <td>{{.Url}}</td>
           <td>{{.Description}}</td>
           <td>
             **{{if ne .Pid 0}}<a href="/permission/edit/{{.Id}}" class="btn btn-xs btn-warning">编辑</a>{{end}}
             {{if ne .Pid 0}}<a href="javascript:if(confirm('确认删除吗?')) location.href='/permission/delete/{{.Id}}'" class="btn btn-xs btn-danger">删除</a>{{end}}**
           </td>
         </tr>
         {{end}}
</tbody>

功能改进,为了让新手更好的理解beego框架的orm,我把sql语句全部改成了orm操作:
改进1:
//删除角色权限

func DeleteRolePermissionByPermissionId(permission_id int) { 
    o := orm.NewOrm() 
    permission := Permission{Id:permission_id} 
    o.QueryM2M(&permission,"Roles").Clear() 
} 

改进2:
//删除文章的回复

func DeleteReplyByTopic(topic *Topic) { 

o := orm.NewOrm() 

replies := Reply{Topic:topic} 

o.Delete(&replies) 
} 

改进3:
//通过用户ID删除回复

func DeleteReplyByUser(user *User) { 

o := orm.NewOrm() 

reply := Reply{User:user} 

o.Delete(&reply) 
} 

改进4:
//通过角色ID删除旧的角色权限

func DeleteRolePermissionByRoleId(role_id int) { 
    o := orm.NewOrm() 
    role := Role{Id:role_id} 
    o.QueryM2M(&role,"Permissions").Clear() 
} 

改进5:
//通过角色id和权限id保存角色权限

func SaveRolePermission(role_id int, permission_id int) { 
    o := orm.NewOrm() 
    role := Role{Id:role_id} 
    permission := Permission{Id:permission_id} 
    o.QueryM2M(&role,"Permissions").Add(&permission) 
} 

改进6:
//通过角色ID查找角色拥有的权限

func FindRolePermissionByRoleId(role_id int) []*Permission { 
    o := orm.NewOrm() 
    role := Role{Id:role_id} 
    o.LoadRelated(&role,"Permissions") 
    return role.Permissions 
} 

改进7:

//通过用户ID来查询权限

func FindPermissionByUser(id int) []*Permission { 

o := orm.NewOrm() 

var permissions []*Permission 

o.QueryTable("permission").Filter("Roles__Role__Users__User__Id",id).All(&permissions) 

return permissions 
} 

改进8:
//查询用户是否有这个权限

func FindPermissionByUserIdAndPermissionName(userId int, name string) bool { 

o := orm.NewOrm() 

var permission Permission 

o.QueryTable("permission").Filter("Roles__Role__Users__User__Id",userId).Filter("Name",name).All(&permission) 

return permission.Id > 0 //如果能查到数据,那么id肯定大于0 
} 
 

改进9:
//通过用户ID删除用户权限方法1

func DeleteUserRolesByUserId(user_id int) { 

o := orm.NewOrm() 

user := User{Id:user_id} 

o.QueryM2M(&user,"Roles").Clear() //清除所有原始关系 
} 

改进10:
//保存用户权限

func SaveUserRole(user_id int, role_id int) { 

o := orm.NewOrm() 

user := User{Id:user_id} 

role := Role{Id:role_id} 

o.QueryM2M(&user,"Roles").Add(&role) 
} 

改进11:
//通过用户ID查找用户角色

func FindUserRolesByUserId(user_id int) []*Role { 

o := orm.NewOrm() 

user := User{Id:user_id} 

o.LoadRelated(&user,"Roles") 

return user.Roles 
}
@atjiu
Copy link
Owner
atjiu commented Feb 15, 2017

bug1,2,4都是没问题的,bug3的建议我已经加上了,谢谢

@atjiu atjiu added the bug label Feb 15, 2017
@atjiu atjiu closed this as completed Feb 15, 2017
@atjiu atjiu reopened this Feb 15, 2017
@atjiu
Copy link
Owner
atjiu commented Feb 15, 2017

改进的部分都是将sql实现改成orm实现,这个对程序没影响,就不改了,已经在readme里说明了

@atjiu atjiu closed this as completed Feb 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants