Exemple #1
0
// 获取单个用户信息
func FindUserByUsername(username string) *model.User {
	user := model.NewUser()
	err := user.Where("username="******"获取用户 %s 信息失败:%s", username, err)
		return nil
	}
	if user.Uid == 0 {
		return nil
	}

	// 获取角色信息
	userRoleList, err := model.NewUserRole().Where("uid=" + strconv.Itoa(user.Uid)).FindAll()
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", username, err)
		return nil
	}
	for _, userRole := range userRoleList {
		if len(user.Roleids) == 0 {
			user.Rolenames = []string{model.AllRole[userRole.Roleid].Name}
		} else {
			user.Rolenames = append(user.Rolenames, model.AllRole[userRole.Roleid].Name)
		}
	}
	return user
}
Exemple #2
0
// 通过唯一键(uid或username)获取用户信息
func findUserByUniq(field, val string) *model.User {
	user := model.NewUser()
	err := user.Where(field + "=" + val).Find()
	if err != nil {
		logger.Errorf("获取用户 %s 信息失败:%s", val, err)
		return nil
	}
	if user.Uid == 0 {
		return nil
	}

	// 获取用户角色信息
	userRoleList, err := model.NewUserRole().
		Order("roleid ASC").Where("uid="+strconv.Itoa(user.Uid)).FindAll("uid", "roleid")
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", val, err)
		return nil
	}

	if roleNum := len(userRoleList); roleNum > 0 {
		user.Roleids = make([]int, roleNum)
		user.Rolenames = make([]string, roleNum)

		for i, userRole := range userRoleList {
			user.Roleids[i] = userRole.Roleid
			user.Rolenames[i] = Roles[userRole.Roleid-1].Name
		}
	}

	return user
}
Exemple #3
0
// 获取当前登录用户信息(常用信息)
func FindCurrentUser(username string) (user map[string]interface{}, err error) {
	userLogin := model.NewUserLogin()
	err = userLogin.Where("username="******"获取用户 %s 信息失败:%s", username, err)
		return
	}
	if userLogin.Uid == 0 {
		logger.Infof("用户 %s 不存在!", username)
		return
	}
	user = map[string]interface{}{
		"uid":      userLogin.Uid,
		"username": userLogin.Username,
		"email":    userLogin.Email,
	}

	// 获取未读消息数
	user["msgnum"] = FindNotReadMsgNum(userLogin.Uid)

	// 获取角色信息
	userRoleList, err := model.NewUserRole().Where("uid=" + strconv.Itoa(userLogin.Uid)).FindAll()
	if err != nil {
		logger.Errorf("获取用户 %s 角色 信息失败:%s", username, err)
		return
	}
	for _, userRole := range userRoleList {
		if userRole.Roleid <= model.AdminMinRoleId {
			// 是管理员
			user["isadmin"] = true
		}
	}
	return
}
Exemple #4
0
func FindUsers() (map[int]*model.User, error) {
	userList, err := model.NewUser().FindAll()
	if err != nil {
		logger.Errorln("user service FindUsers Error:", err)
		return nil, err
	}
	userCount := len(userList)
	userMap := make(map[int]*model.User, userCount)
	uids := make([]int, userCount)
	for i, user := range userList {
		userMap[user.Uid] = user
		uids[i] = user.Uid
	}
	// 获得角色信息
	userRoleList, err := model.NewUserRole().Where("uid in (" + util.Join(uids, ",") + ")").FindAll()
	if err != nil {
		logger.Errorln("user service FindUsers Error:", err)
		return nil, err
	}
	for _, userRole := range userRoleList {
		user := userMap[userRole.Uid]
		if len(user.Roleids) == 0 {
			user.Roleids = []int{userRole.Roleid}
			user.Rolenames = []string{model.AllRole[userRole.Roleid].Name}
		} else {
			user.Roleids = append(user.Roleids, userRole.Roleid)
			user.Rolenames = append(user.Rolenames, model.AllRole[userRole.Roleid].Name)
		}
	}
	return userMap, nil
}
Exemple #5
0
func CreateUser(form url.Values) (errMsg string, err error) {
	// 存用户基本信息,产生自增长UID
	user := model.NewUser()
	err = util.ConvertAssign(user, form)
	if err != nil {
		logger.Errorln("user ConvertAssign error", err)
		errMsg = err.Error()
		return
	}
	uid, err := user.Insert()
	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln(errMsg, ":", err)
		return
	}

	// 存用户登录信息
	userLogin := model.NewUserLogin()
	err = util.ConvertAssign(userLogin, form)
	if err != nil {
		errMsg = err.Error()
		logger.Errorln("CreateUser error:", err)
		return
	}
	userLogin.Uid = uid
	_, err = userLogin.Insert()
	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln(errMsg, ":", err)
		return
	}

	// 存用户角色信息
	userRole := model.NewUserRole()
	// 默认为初级会员
	roleId := model.AllRoleId[len(model.AllRoleId)-1]
	if form.Get("roleid") != "" {
		tmpId, err := strconv.Atoi(form.Get("roleid"))
		if err == nil {
			roleId = tmpId
		}
	}
	userRole.Roleid = roleId
	userRole.Uid = uid
	if _, err = userRole.Insert(); err != nil {
		logger.Errorln("userRole insert Error:", err)
	}

	// 存用户活跃信息,初始活跃+2
	userActive := model.NewUserActive()
	userActive.Uid = uid
	userActive.Username = user.Username
	userActive.Email = user.Email
	userActive.Weight = 2
	if _, err = userActive.Insert(); err != nil {
		logger.Errorln("UserActive insert Error:", err)
	}
	return
}
Exemple #6
0
func AllocUserRoles(uid int, roleids []string) error {
	userRole := model.NewUserRole()
	userRole.Uid = uid

	for _, roleId := range roleids {
		userRole.Roleid, _ = strconv.Atoi(roleId)
		if userRole.Roleid == 0 {
			continue
		}

		userRole.Insert()
	}

	return nil
}
Exemple #7
0
func userAuthority(uid string) (map[int]bool, error) {
	userRoles, err := model.NewUserRole().Where("uid=" + uid).FindAll("roleid")
	if err != nil {
		logger.Errorln("userAuthority userole read fail:", err)
		return nil, err
	}

	roleAuthLocker.RLock()

	aidMap := make(map[int]bool)
	for _, userRole := range userRoles {
		for _, aid := range RoleAuthorities[userRole.Roleid] {
			aidMap[aid] = true
		}
	}

	roleAuthLocker.RUnlock()

	return aidMap, nil
}
Exemple #8
0
func CreateUser(form url.Values) (errMsg string, err error) {
	if EmailExists(form.Get("email")) {
		err = errors.New("该邮箱已注册过")
		return
	}
	if UsernameExists(form.Get("username")) {
		err = errors.New("用户名已存在")
		return
	}
	// 存用户基本信息,产生自增长UID
	user := model.NewUser()
	err = util.ConvertAssign(user, form)
	if err != nil {
		logger.Errorln("user ConvertAssign error", err)
		errMsg = err.Error()
		return
	}
	user.Ctime = util.TimeNow()

	// 随机给一个默认头像
	user.Avatar = DefaultAvatars[rand.Intn(len(DefaultAvatars))]
	uid, err := user.Insert()
	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln(errMsg, ":", err)
		return
	}

	// 存用户登录信息
	userLogin := model.NewUserLogin()
	err = util.ConvertAssign(userLogin, form)
	if err != nil {
		errMsg = err.Error()
		logger.Errorln("CreateUser error:", err)
		return
	}
	userLogin.Uid = uid
	_, err = userLogin.Insert()
	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln(errMsg, ":", err)
		return
	}

	// 存用户角色信息
	userRole := model.NewUserRole()
	// 默认为初级会员
	userRole.Roleid = Roles[len(Roles)-1].Roleid
	userRole.Uid = uid
	if _, err = userRole.Insert(); err != nil {
		logger.Errorln("userRole insert Error:", err)
	}

	// 存用户活跃信息,初始活跃+2
	userActive := model.NewUserActive()
	userActive.Uid = uid
	userActive.Username = user.Username
	userActive.Avatar = user.Avatar
	userActive.Email = user.Email
	userActive.Weight = 2
	if _, err = userActive.Insert(); err != nil {
		logger.Errorln("UserActive insert Error:", err)
	}
	return
}