func (*GroupsController) preCheckUser(ctx *gin.Context, paramJSON *paramUserJSON) (models.Group, error) { usernameExists := models.IsUsernameExists(paramJSON.Username) group := models.Group{} if !usernameExists { e := errors.New("username " + paramJSON.Username + " does not exist") ctx.AbortWithError(http.StatusInternalServerError, e) return group, e } errfinding := group.FindByName(paramJSON.Groupname) if errfinding != nil { ctx.AbortWithError(http.StatusInternalServerError, errfinding) return group, errfinding } if utils.IsTatAdmin(ctx) { // if Tat admin, ok return group, nil } user, err := PreCheckUser(ctx) if err != nil { return models.Group{}, err } if !group.IsUserAdmin(&user) { e := fmt.Errorf("user %s is not admin on group %s", user.Username, group.Name) ctx.AbortWithError(http.StatusInternalServerError, e) return models.Group{}, e } return group, nil }
func (t *TopicsController) preCheckUser(ctx *gin.Context, paramJSON *paramTopicUserJSON) (models.Topic, error) { usernameExists := models.IsUsernameExists(paramJSON.Username) if !usernameExists { e := errors.New("username " + paramJSON.Username + " does not exist") ctx.AbortWithError(http.StatusInternalServerError, e) return models.Topic{}, e } return t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic) }
// Create a new user, record Username, Fullname and Email // A mail is sent to ask user for validation func (u *UsersController) Create(ctx *gin.Context) { var userJSON userCreateJSON ctx.Bind(&userJSON) var userIn models.User userIn.Username = u.computeUsername(userJSON) userIn.Fullname = strings.TrimSpace(userJSON.Fullname) userIn.Email = strings.TrimSpace(userJSON.Email) callback := strings.TrimSpace(userJSON.Callback) if len(userIn.Username) < 3 || len(userIn.Fullname) < 3 || len(userIn.Email) < 7 { err := fmt.Errorf("Invalid username (%s) or fullname (%s) or email (%s)", userIn.Username, userIn.Fullname, userIn.Email) AbortWithReturnError(ctx, http.StatusInternalServerError, err) return } err := u.checkAllowedDomains(userJSON) if err != nil { ctx.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) return } if models.IsEmailExists(userJSON.Email) || models.IsUsernameExists(userJSON.Username) || models.IsFullnameExists(userJSON.Fullname) { e := fmt.Errorf("Please check your username, email or fullname. If you are already registered, please reset your password") AbortWithReturnError(ctx, http.StatusBadRequest, e) return } tokenVerify, err := userIn.Insert() if err != nil { log.Errorf("Error while InsertUser %s", err) ctx.AbortWithError(http.StatusInternalServerError, err) return } go utils.SendVerifyEmail(userIn.Username, userIn.Email, tokenVerify, callback) go models.WSUser(&models.WSUserJSON{Action: "create", Username: userIn.Username}) info := "" if viper.GetBool("username_from_email") { info = fmt.Sprintf(" Note that configuration of Tat forced your username to %s", userIn.Username) } ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("please check your mail to validate your account.%s", info)}) }