func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) { ctx.Data["Title"] = ctx.Org.Organization.FullName ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamsNew"] = true t := &models.Team{ OrgID: ctx.Org.Organization.ID, Name: form.TeamName, Description: form.Description, Authorize: models.ParseAccessMode(form.Permission), } ctx.Data["Team"] = t if ctx.HasError() { ctx.HTML(200, TEAM_NEW) return } if err := models.NewTeam(t); err != nil { ctx.Data["Err_TeamName"] = true switch { case models.IsErrTeamAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form) default: ctx.Handle(500, "NewTeam", err) } return } log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName) }
func NewMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) { ctx.Data["Title"] = ctx.Tr("repo.milestones.new") ctx.Data["PageIsIssueList"] = true ctx.Data["PageIsMilestones"] = true ctx.Data["RequireDatetimepicker"] = true ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language()) if ctx.HasError() { ctx.HTML(200, MILESTONE_NEW) return } if len(form.Deadline) == 0 { form.Deadline = "9999-12-31" } deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local) if err != nil { ctx.Data["Err_Deadline"] = true ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &form) return } if err = models.NewMilestone(&models.Milestone{ RepoID: ctx.Repo.Repository.ID, Name: form.Title, Content: form.Content, Deadline: deadline, }); err != nil { ctx.Handle(500, "NewMilestone", err) return } ctx.Flash.Success(ctx.Tr("repo.milestones.create_success", form.Title)) ctx.Redirect(ctx.Repo.RepoLink + "/milestones") }
func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsPassword"] = true if ctx.HasError() { ctx.HTML(200, SETTINGS_PASSWORD) return } if !ctx.User.ValidatePassword(form.OldPassword) { ctx.Flash.Error(ctx.Tr("settings.password_incorrect")) } else if form.Password != form.Retype { ctx.Flash.Error(ctx.Tr("form.password_not_match")) } else { ctx.User.Passwd = form.Password ctx.User.Salt = models.GetUserSalt() ctx.User.EncodePasswd() if err := models.UpdateUser(ctx.User); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User password updated: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.change_password_success")) } ctx.Redirect(setting.AppSubUrl + "/user/settings/password") }
func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsApplications"] = true if ctx.HasError() { tokens, err := models.ListAccessTokens(ctx.User.ID) if err != nil { ctx.Handle(500, "ListAccessTokens", err) return } ctx.Data["Tokens"] = tokens ctx.HTML(200, SETTINGS_APPLICATIONS) return } t := &models.AccessToken{ UID: ctx.User.ID, Name: form.Name, } if err := models.NewAccessToken(t); err != nil { ctx.Handle(500, "NewAccessToken", err) return } ctx.Flash.Success(ctx.Tr("settings.generate_token_succees")) ctx.Flash.Info(t.Sha1) ctx.Redirect(setting.AppSubUrl + "/user/settings/applications") }
func CreatePost(ctx *context.Context, form auth.CreateOrgForm) { ctx.Data["Title"] = ctx.Tr("new_org") if ctx.HasError() { ctx.HTML(200, CREATE) return } org := &models.User{ Name: form.OrgName, IsActive: true, Type: models.USER_TYPE_ORGANIZATION, } if err := models.CreateOrganization(org, ctx.User); err != nil { ctx.Data["Err_OrgName"] = true switch { case models.IsErrUserAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form) case models.IsErrNameReserved(err): ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form) case models.IsErrNamePatternNotAllowed(err): ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form) default: ctx.Handle(500, "CreateOrganization", err) } return } log.Trace("Organization created: %s", org.Name) ctx.Redirect(setting.AppSubUrl + "/org/" + form.OrgName + "/dashboard") }
func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsProfile"] = true if ctx.HasError() { ctx.HTML(200, SETTINGS_PROFILE) return } handleUsernameChange(ctx, form.Name) if ctx.Written() { return } ctx.User.FullName = form.FullName ctx.User.Email = form.Email ctx.User.Website = form.Website ctx.User.Location = form.Location if err := models.UpdateUser(ctx.User); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User settings updated: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) ctx.Redirect(setting.AppSubUrl + "/user/settings") }
func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) { if ctx.HasError() { ctx.Redirect(ctx.Repo.RepoLink + "/labels") return } list, err := models.GetLabelTemplateFile(form.TemplateName) if err != nil { ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, err)) ctx.Redirect(ctx.Repo.RepoLink + "/labels") return } labels := make([]*models.Label, len(list)) for i := 0; i < len(list); i++ { labels[i] = &models.Label{ RepoID: ctx.Repo.Repository.ID, Name: list[i][0], Color: list[i][1], } } if err := models.NewLabels(labels...); err != nil { ctx.Handle(500, "NewLabels", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/labels") }
func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) { ctx.Data["Title"] = ctx.Tr("admin.users.edit_account") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminUsers"] = true u := prepareUserInfo(ctx) if ctx.Written() { return } if ctx.HasError() { ctx.HTML(200, USER_EDIT) return } fields := strings.Split(form.LoginType, "-") if len(fields) == 2 { loginType := models.LoginType(com.StrTo(fields[0]).MustInt()) loginSource := com.StrTo(fields[1]).MustInt64() if u.LoginSource != loginSource { u.LoginSource = loginSource u.LoginType = loginType } } 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 u.MaxRepoCreation = form.MaxRepoCreation u.IsActive = form.Active u.IsAdmin = form.Admin u.AllowGitHook = form.AllowGitHook u.AllowImportLocal = form.AllowImportLocal u.ProhibitLogin = form.ProhibitLogin if err := models.UpdateUser(u); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form) } else { ctx.Handle(500, "UpdateUser", err) } return } log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success")) ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid")) }
func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsEmails"] = true // Make emailaddress primary. if ctx.Query("_method") == "PRIMARY" { if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil { ctx.Handle(500, "MakeEmailPrimary", err) return } log.Trace("Email made primary: %s", ctx.User.Name) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") return } // Add Email address. emails, err := models.GetEmailAddresses(ctx.User.ID) if err != nil { ctx.Handle(500, "GetEmailAddresses", err) return } ctx.Data["Emails"] = emails if ctx.HasError() { ctx.HTML(200, SETTINGS_EMAILS) return } email := &models.EmailAddress{ UID: ctx.User.ID, Email: form.Email, IsActivated: !setting.Service.RegisterEmailConfirm, } if err := models.AddEmailAddress(email); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form) return } ctx.Handle(500, "AddEmailAddress", err) return } // Send confirmation email if setting.Service.RegisterEmailConfirm { models.SendActivateEmailMail(ctx.Context, ctx.User, email) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60)) } else { ctx.Flash.Success(ctx.Tr("settings.add_email_success")) } log.Trace("Email address added: %s", email.Email) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") }
func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = ctx.Tr("admin.auths.new") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(form.Type)] ctx.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)] ctx.Data["AuthSources"] = authSources ctx.Data["SecurityProtocols"] = securityProtocols ctx.Data["SMTPAuths"] = models.SMTPAuths hasTLS := false var config core.Conversion switch models.LoginType(form.Type) { case models.LOGIN_LDAP, models.LOGIN_DLDAP: config = parseLDAPConfig(form) hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SECURITY_PROTOCOL_UNENCRYPTED case models.LOGIN_SMTP: config = parseSMTPConfig(form) hasTLS = true case models.LOGIN_PAM: config = &models.PAMConfig{ ServiceName: form.PAMServiceName, } default: ctx.Error(400) return } ctx.Data["HasTLS"] = hasTLS if ctx.HasError() { ctx.HTML(200, AUTH_NEW) return } if err := models.CreateLoginSource(&models.LoginSource{ Type: models.LoginType(form.Type), Name: form.Name, IsActived: form.IsActive, Cfg: config, }); err != nil { if models.IsErrLoginSourceAlreadyExist(err) { ctx.Data["Err_Name"] = true ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), AUTH_NEW, form) } else { ctx.Handle(500, "CreateSource", err) } return } log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name) ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name)) ctx.Redirect(setting.AppSubUrl + "/admin/auths") }
func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) { ctx.Data["Title"] = ctx.Tr("repo.release.new_release") ctx.Data["PageIsReleaseList"] = true if ctx.HasError() { ctx.HTML(200, RELEASE_NEW) return } if !ctx.Repo.GitRepo.IsBranchExist(form.Target) { ctx.RenderWithErr(ctx.Tr("form.target_branch_not_exist"), RELEASE_NEW, &form) return } commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target) if err != nil { ctx.Handle(500, "GetBranchCommit", err) return } commitsCount, err := commit.CommitsCount() if err != nil { ctx.Handle(500, "CommitsCount", err) return } rel := &models.Release{ RepoID: ctx.Repo.Repository.ID, PublisherID: ctx.User.ID, Title: form.Title, TagName: form.TagName, Target: form.Target, Sha1: commit.ID.String(), NumCommits: commitsCount, Note: form.Content, IsDraft: len(form.Draft) > 0, IsPrerelease: form.Prerelease, } if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil { ctx.Data["Err_TagName"] = true switch { case models.IsErrReleaseAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &form) case models.IsErrInvalidTagName(err): ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &form) default: ctx.Handle(500, "CreateRelease", err) } return } log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) ctx.Redirect(ctx.Repo.RepoLink + "/releases") }
func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) { ctx.Data["Title"] = ctx.Tr("org.settings") ctx.Data["PageIsSettingsOptions"] = true if ctx.HasError() { ctx.HTML(200, SETTINGS_OPTIONS) return } org := ctx.Org.Organization // Check if organization name has been changed. if org.LowerName != strings.ToLower(form.Name) { isExist, err := models.IsUserExist(org.Id, form.Name) if err != nil { ctx.Handle(500, "IsUserExist", err) return } else if isExist { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form) return } else if err = models.ChangeUserName(org, form.Name); err != nil { if err == models.ErrUserNameIllegal { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form) } else { ctx.Handle(500, "ChangeUserName", err) } return } // reset ctx.org.OrgLink with new name ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + form.Name log.Trace("Organization name changed: %s -> %s", org.Name, form.Name) } // In case it's just a case change. org.Name = form.Name org.LowerName = strings.ToLower(form.Name) if ctx.User.IsAdmin { org.MaxRepoCreation = form.MaxRepoCreation } org.FullName = form.FullName org.Description = form.Description org.Website = form.Website org.Location = form.Location if err := models.UpdateUser(org); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("Organization setting updated: %s", org.Name) ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success")) ctx.Redirect(ctx.Org.OrgLink + "/settings") }
func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) { ctx.Data["PageIsDelete"] = true ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName ctx.Data["TreePath"] = ctx.Repo.TreePath oldBranchName := ctx.Repo.BranchName branchName := oldBranchName if form.CommitChoice == "commit-to-new-branch" { branchName = form.NewBranchName } ctx.Data["commit_summary"] = form.CommitSummary ctx.Data["commit_message"] = form.CommitMessage ctx.Data["commit_choice"] = form.CommitChoice ctx.Data["new_branch_name"] = branchName if ctx.HasError() { ctx.HTML(200, DELETE_FILE) return } if oldBranchName != branchName { if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil { ctx.Data["Err_NewBranchName"] = true ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), DELETE_FILE, &form) return } } message := strings.TrimSpace(form.CommitSummary) if len(message) == 0 { message = ctx.Tr("repo.editor.delete", ctx.Repo.TreePath) } form.CommitMessage = strings.TrimSpace(form.CommitMessage) if len(form.CommitMessage) > 0 { message += "\n\n" + form.CommitMessage } if err := ctx.Repo.Repository.DeleteRepoFile(ctx.User, models.DeleteRepoFileOptions{ LastCommitID: ctx.Repo.CommitID, OldBranch: oldBranchName, NewBranch: branchName, TreePath: ctx.Repo.TreePath, Message: message, }); err != nil { ctx.Handle(500, "DeleteRepoFile", err) return } ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath)) ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName) }
func ForkPost(ctx *context.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_fork") forkRepo := getForkRepository(ctx) if ctx.Written() { return } ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if ctx.HasError() { ctx.HTML(200, FORK) return } repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID) if has { ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } // Check ownership of organization. if ctxUser.IsOrganization() { if !ctxUser.IsOwnedBy(ctx.User.Id) { ctx.Error(403) return } } repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description) if err != nil { ctx.Data["Err_RepoName"] = true switch { case models.IsErrRepoAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form) case models.IsErrNameReserved(err): ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) case models.IsErrNamePatternNotAllowed(err): ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form) default: ctx.Handle(500, "ForkPost", err) } return } log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) }
func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} orCtx, err := getOrgRepoCtx(ctx) if err != nil { ctx.Handle(500, "getOrgRepoCtx", err) return } if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } meta, err := json.Marshal(&models.SlackMeta{ Channel: form.Channel, Username: form.Username, IconURL: form.IconURL, Color: form.Color, }) if err != nil { ctx.Handle(500, "Marshal", err) return } w := &models.Webhook{ RepoID: orCtx.RepoID, URL: form.PayloadURL, ContentType: models.JSON, HookEvent: ParseHookEvent(form.WebhookForm), IsActive: form.Active, HookTaskType: models.SLACK, Meta: string(meta), OrgID: orCtx.OrgID, } if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.CreateWebhook(w); err != nil { ctx.Handle(500, "CreateWebhook", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) ctx.Redirect(orCtx.Link + "/settings/hooks") }
func DeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) { ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys") ctx.Data["PageIsSettingsKeys"] = true keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID) if err != nil { ctx.Handle(500, "ListDeployKeys", err) return } ctx.Data["Deploykeys"] = keys if ctx.HasError() { ctx.HTML(200, DEPLOY_KEYS) return } content, err := models.CheckPublicKeyString(form.Content) if err != nil { if models.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Data["HasError"] = true ctx.Data["Err_Content"] = true ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys") return } } key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) if err != nil { ctx.Data["HasError"] = true switch { case models.IsErrKeyAlreadyExist(err): ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form) case models.IsErrKeyNameAlreadyUsed(err): ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form) default: ctx.Handle(500, "AddDeployKey", err) } return } log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID) ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name)) ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys") }
func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = ctx.Tr("admin.auths.edit") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["SMTPAuths"] = models.SMTPAuths source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) if err != nil { ctx.Handle(500, "GetLoginSourceByID", err) return } ctx.Data["Source"] = source ctx.Data["HasTLS"] = source.HasTLS() if ctx.HasError() { ctx.HTML(200, AUTH_EDIT) return } var config core.Conversion switch models.LoginType(form.Type) { case models.LOGIN_LDAP, models.LOGIN_DLDAP: config = parseLDAPConfig(form) case models.LOGIN_SMTP: config = parseSMTPConfig(form) case models.LOGIN_PAM: config = &models.PAMConfig{ ServiceName: form.PAMServiceName, } default: ctx.Error(400) return } source.Name = form.Name source.IsActived = form.IsActive source.Cfg = config if err := models.UpdateSource(source); err != nil { ctx.Handle(500, "UpdateSource", err) return } log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID) ctx.Flash.Success(ctx.Tr("admin.auths.update_success")) ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID)) }
func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) { t := ctx.Org.Team ctx.Data["Title"] = ctx.Org.Organization.FullName ctx.Data["PageIsOrgTeams"] = true ctx.Data["Team"] = t if ctx.HasError() { ctx.HTML(200, TEAM_NEW) return } isAuthChanged := false if !t.IsOwnerTeam() { // Validate permission level. var auth models.AccessMode switch form.Permission { case "read": auth = models.ACCESS_MODE_READ case "write": auth = models.ACCESS_MODE_WRITE case "admin": auth = models.ACCESS_MODE_ADMIN default: ctx.Error(401) return } t.Name = form.TeamName if t.Authorize != auth { isAuthChanged = true t.Authorize = auth } } t.Description = form.Description if err := models.UpdateTeam(t, isAuthChanged); err != nil { ctx.Data["Err_TeamName"] = true switch { case models.IsErrTeamAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form) default: ctx.Handle(500, "UpdateTeam", err) } return } ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName) }
func EditWikiPost(ctx *context.Context, form auth.NewWikiForm) { ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page") ctx.Data["PageIsWiki"] = true ctx.Data["RequireSimpleMDE"] = true if ctx.HasError() { ctx.HTML(200, WIKI_NEW) return } if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil { ctx.Handle(500, "EditWikiPage", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title)) }
func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} ctx.Data["HookType"] = "gogs" orCtx, err := getOrgRepoCtx(ctx) if err != nil { ctx.Handle(500, "getOrgRepoCtx", err) return } ctx.Data["BaseLink"] = orCtx.Link if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } contentType := models.JSON if models.HookContentType(form.ContentType) == models.FORM { contentType = models.FORM } w := &models.Webhook{ RepoID: orCtx.RepoID, URL: form.PayloadURL, ContentType: contentType, Secret: form.Secret, HookEvent: ParseHookEvent(form.WebhookForm), IsActive: form.Active, HookTaskType: models.GOGS, OrgID: orCtx.OrgID, } if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.CreateWebhook(w); err != nil { ctx.Handle(500, "CreateWebhook", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) ctx.Redirect(orCtx.Link + "/settings/hooks") }
func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) { ctx.Data["Title"] = ctx.Org.Organization.FullName ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamsNew"] = true // Validate permission level. var auth models.AccessMode switch form.Permission { case "read": auth = models.ACCESS_MODE_READ case "write": auth = models.ACCESS_MODE_WRITE case "admin": auth = models.ACCESS_MODE_ADMIN default: ctx.Error(401) return } t := &models.Team{ OrgID: ctx.Org.Organization.Id, Name: form.TeamName, Description: form.Description, Authorize: auth, } ctx.Data["Team"] = t if ctx.HasError() { ctx.HTML(200, TEAM_NEW) return } if err := models.NewTeam(t); err != nil { ctx.Data["Err_TeamName"] = true switch { case models.IsErrTeamAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form) default: ctx.Handle(500, "NewTeam", err) } return } log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName) }
func SettingsSSHKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsSSHKeys"] = true keys, err := models.ListPublicKeys(ctx.User.ID) if err != nil { ctx.Handle(500, "ListPublicKeys", err) return } ctx.Data["Keys"] = keys if ctx.HasError() { ctx.HTML(200, SETTINGS_SSH_KEYS) return } content, err := models.CheckPublicKeyString(form.Content) if err != nil { if models.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh") return } } if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil { ctx.Data["HasError"] = true switch { case models.IsErrKeyAlreadyExist(err): ctx.Data["Err_Content"] = true ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form) case models.IsErrKeyNameAlreadyUsed(err): ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &form) default: ctx.Handle(500, "AddPublicKey", err) } return } ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title)) ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh") }
func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true renderAttachmentSettings(ctx) var ( repo = ctx.Repo.Repository attachments []string ) labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form) if ctx.Written() { return } if setting.AttachmentEnabled { attachments = form.Attachments } if ctx.HasError() { ctx.HTML(200, ISSUE_NEW) return } issue := &models.Issue{ RepoID: repo.ID, Name: form.Title, PosterID: ctx.User.Id, Poster: ctx.User, MilestoneID: milestoneID, AssigneeID: assigneeID, Content: form.Content, } if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil { ctx.Handle(500, "NewIssue", err) return } else if err := MailWatchersAndMentions(ctx, issue); err != nil { ctx.Handle(500, "MailWatchersAndMentions", err) return } log.Trace("Issue created: %d/%d", repo.ID, issue.ID) ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index)) }
func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = ctx.Tr("admin.auths.new") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)] ctx.Data["AuthSources"] = authSources ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, AUTH_NEW) return } var config core.Conversion switch models.LoginType(form.Type) { case models.LOGIN_LDAP, models.LOGIN_DLDAP: config = parseLDAPConfig(form) case models.LOGIN_SMTP: config = parseSMTPConfig(form) case models.LOGIN_PAM: config = &models.PAMConfig{ ServiceName: form.PAMServiceName, } default: ctx.Error(400) return } if err := models.CreateSource(&models.LoginSource{ Type: models.LoginType(form.Type), Name: form.Name, IsActived: form.IsActive, Cfg: config, }); err != nil { ctx.Handle(500, "CreateSource", err) return } log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name) ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name)) ctx.Redirect(setting.AppSubUrl + "/admin/auths") }
func EditMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) { ctx.Data["Title"] = ctx.Tr("repo.milestones.edit") ctx.Data["PageIsMilestones"] = true ctx.Data["PageIsEditMilestone"] = true ctx.Data["RequireDatetimepicker"] = true ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language()) if ctx.HasError() { ctx.HTML(200, MILESTONE_NEW) return } if len(form.Deadline) == 0 { form.Deadline = "9999-12-31" } deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local) if err != nil { ctx.Data["Err_Deadline"] = true ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &form) return } m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Handle(404, "", nil) } else { ctx.Handle(500, "GetMilestoneByRepoID", err) } return } m.Name = form.Title m.Content = form.Content m.Deadline = deadline if err = models.UpdateMilestone(m); err != nil { ctx.Handle(500, "UpdateMilestone", err) return } ctx.Flash.Success(ctx.Tr("repo.milestones.edit_success", m.Name)) ctx.Redirect(ctx.Repo.RepoLink + "/milestones") }
func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true orCtx, w := checkWebhook(ctx) if ctx.Written() { return } ctx.Data["Webhook"] = w if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } meta, err := json.Marshal(&models.SlackMeta{ Channel: form.Channel, Username: form.Username, IconURL: form.IconURL, Color: form.Color, }) if err != nil { ctx.Handle(500, "Marshal", err) return } w.URL = form.PayloadURL w.Meta = string(meta) w.HookEvent = ParseHookEvent(form.WebhookForm) w.IsActive = form.Active if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.UpdateWebhook(w); err != nil { ctx.Handle(500, "UpdateWebhook", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) }
func CreatePost(ctx *context.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_repo") ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctx.Data["Readmes"] = models.Readmes ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if ctx.HasError() { ctx.HTML(200, CREATE) return } repo, err := models.CreateRepository(ctxUser, models.CreateRepoOptions{ Name: form.RepoName, Description: form.Description, Gitignores: form.Gitignores, License: form.License, Readme: form.Readme, IsPrivate: form.Private || setting.Repository.ForcePrivate, AutoInit: form.AutoInit, }) if err == nil { log.Trace("Repository created[%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } if repo != nil { if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } handleCreateError(ctx, ctxUser, err, "CreatePost", CREATE, &form) }
func NewLabel(ctx *context.Context, form auth.CreateLabelForm) { ctx.Data["Title"] = ctx.Tr("repo.labels") ctx.Data["PageIsLabels"] = true if ctx.HasError() { ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) ctx.Redirect(ctx.Repo.RepoLink + "/labels") return } l := &models.Label{ RepoID: ctx.Repo.Repository.ID, Name: form.Title, Color: form.Color, } if err := models.NewLabels(l); err != nil { ctx.Handle(500, "NewLabel", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/labels") }
func SignInPost(ctx *context.Context, form auth.SignInForm) { ctx.Data["Title"] = ctx.Tr("sign_in") if ctx.HasError() { ctx.HTML(200, SIGNIN) return } u, err := models.UserSignIn(form.UserName, form.Password) if err != nil { if models.IsErrUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form) } else { ctx.Handle(500, "UserSignIn", err) } return } if form.Remember { days := 86400 * setting.LogInRememberDays ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubUrl) ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName, u.Name, days, setting.AppSubUrl) } ctx.Session.Set("uid", u.Id) ctx.Session.Set("uname", u.Name) // Clear whatever CSRF has right now, force to generate a new one ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubUrl) if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { ctx.SetCookie("redirect_to", "", -1, setting.AppSubUrl) ctx.Redirect(redirectTo) return } ctx.Redirect(setting.AppSubUrl + "/") }
func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) { ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page") ctx.Data["PageIsWiki"] = true ctx.Data["RequireSimpleMDE"] = true if ctx.HasError() { ctx.HTML(200, WIKI_NEW) return } if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil { if models.IsErrWikiAlreadyExist(err) { ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &form) } else { ctx.Handle(500, "AddWikiPage", err) } return } ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title)) }