Example #1
0
func (self *AuthorityResource) Get(ctx *rfweb.Context) {
	res := RespData{}
	id := ctx.Get("id")
	if id == "all" {
		authoritys, err := auth.GetAllAuthority()
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = authoritys
		}
	} else if id != "" {
		authority, err := auth.GetAuthorityByID(id)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = authority
		}
	} else {
		email := ctx.Get("email")
		if email != "" {
			authority, err := auth.GetAuthorityByEmail(email)
			if err != nil {
				res.Error = err.Error()
			} else {
				res.Success = true
				res.Data = authority
			}
		}
	}

	util.WriteJson(ctx.W, res)
}
Example #2
0
// check permission
func (self *BaseResource) OnHandleBegin(ctx *rfweb.Context) bool {
	user := auth.GetLoginedUser(ctx)
	errMsg := ""
	if user == nil || user.Email == "" {
		errMsg = "please login first."
	} else {
		authority, err := auth.GetAuthorityByEmail(user.Email)
		if err != nil {
			errMsg = "can't get authority, error: " + err.Error()
		} else if authority == nil || authority.AdminLevel < 10 {
			errMsg = "you don't has permission."
		}
	}
	if errMsg != "" {
		isXHR := ctx.R.Header.Get("X-Requested-With") == "XMLHttpRequest"
		if isXHR {
			res := RespData{
				Error: errMsg,
			}
			util.WriteJson(ctx.W, res)
		} else {
			http.Error(ctx.W, errMsg, http.StatusForbidden)
		}
		return false
	}
	return true
}
Example #3
0
// delete role
func (self *RoleResource) Delete(ctx *rfweb.Context) {
	res := RespData{}
	id := ctx.Get("id")
	if len(id) < 1 {
		res.Error = "no id"
	} else {
		err := auth.DeleteRole(id)
		if err == nil {
			res.Success = true
			res.Data = id
		} else {
			res.Error = err.Error()
		}
	}
	util.WriteJson(ctx.W, res)
}
Example #4
0
// update role config
func (self *RoleResource) Put(ctx *rfweb.Context) {
	res := RespData{}
	role, err := getRoleFromBody(ctx.R)
	if err != nil {
		res.Error = err.Error()
	} else {
		err = auth.UpdateRole(role.Id, role)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Data = role
			res.Success = true
			// proxy.RemoveBackendConfCache()
		}
	}
	util.WriteJson(ctx.W, res)
}
Example #5
0
// update user
func (self *UserResource) Put(ctx *rfweb.Context) {
	res := RespData{}
	user, err := getUserFromBody(ctx.R, false)
	if err != nil {
		res.Error = err.Error()
	} else {
		err = auth.UpdateUser(user.Id, *user)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Data = user
			res.Success = true
			// proxy.RemoveBackendConfCache()
		}
	}
	util.WriteJson(ctx.W, res)
}
Example #6
0
// update authority
func (self *AuthorityResource) Put(ctx *rfweb.Context) {
	res := RespData{}
	authority, err := getAuthorityFromBody(ctx.R)
	if err != nil {
		res.Error = err.Error()
	} else {
		err = auth.UpdateAuthority(authority.Id, authority)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Data = authority
			res.Success = true
			// proxy.RemoveBackendConfCache()
		}
	}
	util.WriteJson(ctx.W, res)
}
Example #7
0
// update backend config
func (self *BackendConfResource) Put(ctx *rfweb.Context) {
	res := RespData{}
	bc, err := getBackendConfFromBody(ctx.R)
	if err != nil {
		res.Error = err.Error()
	} else {
		err = bkconf.Update(bc.Id, bc)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Data = bc
			res.Success = true
			proxy.RemoveBackendConfCache()
		}
	}
	util.WriteJson(ctx.W, res)
}
Example #8
0
func (self LoginResource) Post(ctx *rfweb.Context) {
	res := RespData{}
	email := strings.ToLower(ctx.Get("email"))
	email = strings.TrimSpace(email)
	pwd := strings.TrimSpace(ctx.Get("pwd"))
	// remember := ctx.Get("remember")

	user, err := auth.LoginUser(email, pwd)
	if err != nil {
		res.Error = err.Error()
	} else {
		res.Success = true
		// res.Data = ctx.Get("returnurl")
		session := ctx.Session()
		session.SetStuct(constant.SS_KEY_USER, user)
	}
	util.WriteJson(ctx.W, res)
}
Example #9
0
func (self *RoleResource) Get(ctx *rfweb.Context) {
	res := RespData{}
	id := ctx.Get("id")
	if id == "all" {
		roles, err := auth.GetAllRole()
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = roles
		}
	} else {
		role, err := auth.GetRoleByID(id)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = role
		}
	}

	util.WriteJson(ctx.W, res)
}
Example #10
0
func (self *UserResource) Get(ctx *rfweb.Context) {
	res := RespData{}
	email := ctx.Get("email")
	if email == "all" {
		users, err := auth.GetAllUsers()
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = users
		}
	} else {
		user, err := auth.GetUserByEmail(email)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = user
		}
	}

	util.WriteJson(ctx.W, res)
}
Example #11
0
func (self *BackendConfResource) Get(ctx *rfweb.Context) {
	res := RespData{}
	hostname := ctx.Get("hostname")
	if hostname == "all" {
		bcs, err := bkconf.GetAll()
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = bcs
		}
	} else {
		bc, err := bkconf.Get(hostname)
		if err != nil {
			res.Error = err.Error()
		} else {
			res.Success = true
			res.Data = bc
		}
	}

	util.WriteJson(ctx.W, res)
}