Exemplo n.º 1
0
func (p *UserService) createAndInsertUser(userName string, password string, email string, tenanId string, roleId string, company string) (userId string, err error) {
	// var jsondocument interface{}
	currentUser, erro := p.getAllUserByName(userName)
	if erro != nil {
		logrus.Error("get all user by username err is %v", erro)
		return "", erro
	}
	if len(currentUser) != 0 {
		logrus.Infoln("user already exist! username:"******"create user error %v", err)
		return
	}
	userId = user.ObjectId.Hex()

	return
}
Exemplo n.º 2
0
func (p *UserService) UserUpdate(token string, newuser entity.User, userId string) (created bool, id string, errorCode string, err error) {
	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return false, userId, code, err
	}

	if authorized := GetAuthService().Authorize("update_user", token, userId, p.userCollectionName); !authorized {
		logrus.Errorln("required opertion is not allowed!")
		return false, userId, COMMON_ERROR_UNAUTHORIZED, errors.New("Required opertion is not authorized!")
	}

	if !bson.IsObjectIdHex(userId) {
		logrus.Errorf("invalid user id format for user update %v", userId)
		return false, "", COMMON_ERROR_INVALIDATE, errors.New("Invalid object Id for user update")
	}

	selector := bson.M{}
	selector["_id"] = bson.ObjectIdHex(userId)

	queryStruct := dao.QueryStruct{
		CollectionName: p.userCollectionName,
		Selector:       selector,
		Skip:           0,
		Limit:          0,
		Sort:           ""}

	user := new(entity.User)
	err = dao.HandleQueryOne(user, queryStruct)
	if err != nil {
		logrus.Errorf("get user by id error %v", err)
		return false, "", USER_ERROR_UPDATE, err
	}

	if len(newuser.Company) > 0 {
		user.Company = newuser.Company
	}
	if len(newuser.Email) > 0 {
		user.Email = newuser.Email
	}

	user.TimeUpdate = dao.GetCurrentTime()

	created, err = dao.HandleUpdateOne(user, queryStruct)
	return created, userId, "", nil
}