func EditUserRole(userId, roleId int64) (err error) { _, err = dao.GetUserById(userId) if err != nil { Logs.Error("dao getuserbudi err(%v)", err) return } _, err = dao.GetRoleById(roleId) if err != nil { Logs.Error("dao GetRoleById err(%v)", err) return } ur, err := dao.GetUserRoleByUserId(userId) if err != nil && err != orm.ErrNoRows { Logs.Error("dao GetUserRoleByUserId err(%v)", err) return } if err == orm.ErrNoRows { ur = &model.UserRole{} ur.UserId = userId ur.RoleId = roleId _, err = dao.AddUserRole(ur) if err != nil { Logs.Error("dao AddUserRole err(%v)", err) return } return } ur.RoleId = roleId err = dao.UpdateUserRoleById(ur) if err != nil { Logs.Error("dao UpdateUserRoleById err(%v)", err) return } return }
func RoleAdd(r render.Render, req *http.Request) { req.ParseForm() values := req.Form id := values.Get("id") var idInt64 int64 var err error m := &model.Role{} data := make(map[string]interface{}) l, err := dao.GetAllPermission() if err != nil { service.Logs.Error("dao.GetAllPermission() err(%v)", err) } data["l"] = l list := dao.GetFormatPermission() data["list"] = list if id != "" { idInt64, err = strconv.ParseInt(id, 10, 64) if err != nil { service.Logs.Error("strconv.ParseInt err(%v)", err) return } } if req.Method == "GET" { if id != "" && idInt64 != 0 { m, err = dao.GetRoleById(idInt64) if err != nil { service.Logs.Error("dao.GetRoleById err(%v)", err) return } } p := cache.GetRolePermissions(idInt64) data["p"] = p data["m"] = m r.HTML(200, "role_add", data) return } m.Info = values.Get("info") m.Name = values.Get("name") if id == "" || idInt64 == 0 { m.Status = 0 mId, err := dao.AddRole(m) if err != nil { service.Logs.Error("dao.AddRole err(%v)", err) return } m, _ = dao.GetRoleById(mId) } else { m.Id = idInt64 status, _ := strconv.Atoi(values.Get("status")) m.Status = status err = dao.UpdateRoleById(m) if err != nil { service.Logs.Error("dao.UpdateRoleById err(%v)", err) return } } p := cache.GetRolePermissions(idInt64) data["p"] = p data["m"] = m r.HTML(200, "role_add", data) return }