Exemplo n.º 1
0
// createUser creats a user.
func createUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Request
	req := &createUserRequest{
		EnableEmail: true,
		EnablePhone: true,
		Universal:   false,
	}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validation
	if err := models.ValidateUserName(req.Name); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserEmail(req.Email); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserPhone(req.Phone); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	// Save
	user := &models.User{
		Name:        req.Name,
		Email:       req.Email,
		EnableEmail: req.EnableEmail,
		Phone:       req.Phone,
		EnablePhone: req.EnablePhone,
		Universal:   req.Universal,
	}
	if err := db.Admin.DB().Create(user).Error; err != nil {
		// Write errors.
		sqliteErr, ok := err.(sqlite3.Error)
		if ok {
			switch sqliteErr.ExtendedCode {
			case sqlite3.ErrConstraintNotNull:
				ResponseError(w, ErrNotNull)
				return
			case sqlite3.ErrConstraintPrimaryKey:
				ResponseError(w, ErrPrimaryKey)
				return
			case sqlite3.ErrConstraintUnique:
				ResponseError(w, ErrDuplicateUserName)
				return
			}
		}
		// Unexcepted.
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	ResponseJSONOK(w, user)
}
Exemplo n.º 2
0
// Migrate users.
//
//	1. Fetch all users from belldb.
//	2. Create the users into bansheedb.
//	3. Establish the relationships between project and user.
//
func migrateUsers() {
	var users []Receiver
	// Fetch all users from belldb.
	if err := bellDB.Find(&users).Error; err != nil {
		log.Fatal("fetch all users from %s: %v", *bellDBFileName, err)
	}
	for _, user := range users {
		// Create banshee user.
		err := models.ValidateUserName(user.Name)
		if err == nil {
			err = models.ValidateUserEmail(user.Email)
		}
		if err == nil {
			err = models.ValidateUserPhone(user.Phone)
		}
		if err != nil {
			log.Warn("user %s: %v, skipping..", user.Name, err)
		}
		u := &models.User{
			Name:        user.Name,
			Email:       user.Email,
			Phone:       user.Phone,
			EnableEmail: user.EnableEmail,
			EnablePhone: user.EnablePhone,
			Universal:   user.Universal,
		}
		if err := bansheeDB.Create(u).Error; err != nil {
			sqliteErr, ok := err.(sqlite3.Error)
			if ok && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
				log.Warn("user %s already in %s, skipping..", u.Name, *bansheeDBFileName)
			} else {
				log.Fatal("cannot create user %s: %v", u.Name, err)
			}
		}
		// Establish relationship to project.
		if user.Universal {
			continue
		}
		// Get all relationships for this user.
		var relations []ReceiverProject
		if err := bellDB.Where("ReceiverId = ?", user.ID).Find(&relations).Error; err != nil {
			log.Fatal("cannot fetch user-project relations for user %s: %v", user.Name, err)
		}
		for _, relation := range relations {
			var proj Project
			if err := bellDB.First(&proj, relation.ProjectID).Error; err != nil {
				if err == gorm.RecordNotFound {
					log.Warn("project %d not found for user %s, skipping..", relation.ProjectID, user.Name)
					continue
				}
				log.Fatal("cannot get project %d for user %s", relation.ProjectID, user.Name)
			}
			p := &models.Project{}
			if err := bansheeDB.Where("name = ?", proj.Name).First(p).Error; err != nil {
				if err == gorm.RecordNotFound {
					log.Warn("project %s not found in %s, skipping..", proj.Name, *bansheeDBFileName)
					continue
				}
				log.Fatal("cannot get project %s in %s", proj.Name, *bansheeDBFileName)
			}
			if err := bansheeDB.Model(p).Association("Users").Append(u).Error; err != nil {
				if err == gorm.RecordNotFound {
					log.Warn("record not found: %v", err)
					continue
				}
				log.Fatal("cannot append user %s to project %s:%v", u.Name, p.Name, err)
			}
		}
	}
}
Exemplo n.º 3
0
// updateUser updates a user.
func updateUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Params
	id, err := strconv.Atoi(ps.ByName("id"))
	if err != nil {
		ResponseError(w, ErrUserID)
		return
	}
	// Request
	req := &updateUserRequest{}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validation
	if err := models.ValidateUserName(req.Name); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserEmail(req.Email); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserPhone(req.Phone); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateRuleLevel(req.RuleLevel); err != nil {
		ResponseError(w, NewValidationWebError(err))
	}
	// Find
	user := &models.User{}
	if err := db.Admin.DB().First(user, id).Error; err != nil {
		switch err {
		case gorm.RecordNotFound:
			ResponseError(w, ErrUserNotFound)
			return
		default:
			ResponseError(w, NewUnexceptedWebError(err))
			return
		}
	}
	// Patch
	user.Name = req.Name
	user.Email = req.Email
	user.EnableEmail = req.EnableEmail
	user.Phone = req.Phone
	user.EnablePhone = req.EnablePhone
	user.Universal = req.Universal
	user.RuleLevel = req.RuleLevel
	if err := db.Admin.DB().Save(user).Error; err != nil {
		if err == gorm.RecordNotFound {
			// User not found.
			ResponseError(w, ErrUserNotFound)
			return
		}
		// Write errors.
		sqliteErr, ok := err.(sqlite3.Error)
		if ok {
			switch sqliteErr.ExtendedCode {
			case sqlite3.ErrConstraintNotNull:
				ResponseError(w, ErrNotNull)
				return
			case sqlite3.ErrConstraintUnique:
				ResponseError(w, ErrDuplicateUserName)
				return
			}
		}
		// Unexcepted error.
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	ResponseJSONOK(w, user)
}