Exemplo n.º 1
0
func responseApiUsers(ctx *context.APIContext, users []*models.User) {
	apiUsers := make([]*api.User, len(users))
	for i := range users {
		apiUsers[i] = convert.ToUser(users[i])
	}
	ctx.JSON(200, &apiUsers)
}
Exemplo n.º 2
0
// https://github.com/gigforks/go-gogs-client/wiki/Administration-Users#create-a-new-user
func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
	u := &models.User{
		Name:      form.Username,
		Email:     form.Email,
		Passwd:    form.Password,
		IsActive:  true,
		LoginType: models.LOGIN_PLAIN,
	}

	parseLoginSource(ctx, u, form.SourceID, form.LoginName)
	if ctx.Written() {
		return
	}

	if err := models.CreateUser(u); err != nil {
		if models.IsErrUserAlreadyExist(err) ||
			models.IsErrEmailAlreadyUsed(err) ||
			models.IsErrNameReserved(err) ||
			models.IsErrNamePatternNotAllowed(err) {
			ctx.Error(422, "", err)
		} else {
			ctx.Error(500, "CreateUser", err)
		}
		return
	}
	log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)

	// Send e-mail notification.
	if form.SendNotify && setting.MailService != nil {
		mailer.SendRegisterNotifyMail(ctx.Context.Context, u)
	}

	ctx.JSON(201, convert.ToUser(u))
}
Exemplo n.º 3
0
// https://github.com/gigforks/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
func EditUser(ctx *context.APIContext, form api.EditUserOption) {
	u := user.GetUserByParams(ctx)
	if ctx.Written() {
		return
	}

	parseLoginSource(ctx, u, form.SourceID, form.LoginName)
	if ctx.Written() {
		return
	}

	if len(form.Password) > 0 {
		u.Passwd = form.Password
		u.Salt = models.GetUserSalt()
		u.EncodePasswd()
	}

	u.LoginName = form.LoginName
	u.FullName = form.FullName
	u.Email = form.Email
	u.Website = form.Website
	u.Location = form.Location
	if form.Active != nil {
		u.IsActive = *form.Active
	}
	if form.Admin != nil {
		u.IsAdmin = *form.Admin
	}
	if form.AllowGitHook != nil {
		u.AllowGitHook = *form.AllowGitHook
	}
	if form.AllowImportLocal != nil {
		u.AllowImportLocal = *form.AllowImportLocal
	}

	if err := models.UpdateUser(u); err != nil {
		if models.IsErrEmailAlreadyUsed(err) {
			ctx.Error(422, "", err)
		} else {
			ctx.Error(500, "UpdateUser", err)
		}
		return
	}
	log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)

	ctx.JSON(200, convert.ToUser(u))
}
Exemplo n.º 4
0
func ListUserAccess(ctx *context.APIContext) {
	_, repo := parseOwnerAndRepo(ctx)
	if ctx.Written() {
		return
	}

	users, err := repo.GetCollaborators()

	if err != nil {
		ctx.Error(500, "internal server error", err)
		return
	}

	apiUsers := make([]*api.User, 0)

	for _, u := range users {
		apiUsers = append(apiUsers, convert.ToUser(u.User))
	}

	ctx.JSON(200, apiUsers)
}
Exemplo n.º 5
0
func ListUsers(ctx *context.APIContext) {
	page := com.StrTo(ctx.Query("page")).MustInt()
	limit := com.StrTo(ctx.Query("limit")).MustInt()

	if limit <= 0 {
		limit = 10
	} else if limit > 100 {
		limit = 100
	}

	users, err := models.Users(page, limit)
	if err != nil {
		ctx.Error(500, "", err)
	}

	apiUsers := make([]*api.User, 0)
	for _, u := range users {
		apiUsers = append(apiUsers, convert.ToUser(u))
	}

	ctx.JSON(200, apiUsers)
}