func SettingsApplications(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsApplications"] = true // Delete access token. remove, _ := com.StrTo(ctx.Query("remove")).Int64() if remove > 0 { if err := models.DeleteAccessTokenById(remove); err != nil { ctx.Handle(500, "DeleteAccessTokenById", err) return } ctx.Flash.Success(ctx.Tr("settings.delete_token_success")) ctx.Redirect(setting.AppSubUrl + "/user/settings/applications") return } 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) }
func Invitation(ctx *middleware.Context) { org := ctx.Org.Organization ctx.Data["Title"] = org.FullName ctx.Data["PageIsOrgMembers"] = true if ctx.Req.Method == "POST" { uname := ctx.Query("uname") u, err := models.GetUserByName(uname) if err != nil { if err == models.ErrUserNotExist { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/invitations/new") } else { ctx.Handle(500, " GetUserByName", err) } return } if err = org.AddMember(u.Id); err != nil { ctx.Handle(500, " AddMember", err) return } log.Trace("New member added(%s): %s", org.Name, u.Name) ctx.Redirect(ctx.Org.OrgLink + "/members") return } ctx.HTML(200, MEMBER_INVITE) }
func TeamsRepoAction(ctx *middleware.Context) { if !ctx.Org.IsOwner { ctx.Error(404) return } var err error switch ctx.Params(":action") { case "add": repoName := path.Base(ctx.Query("repo-name")) var repo *models.Repository repo, err = models.GetRepositoryByName(ctx.Org.Organization.Id, repoName) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories") return } ctx.Handle(500, "GetRepositoryByName", err) return } err = ctx.Org.Team.AddRepository(repo) case "remove": err = ctx.Org.Team.RemoveRepository(com.StrTo(ctx.Query("repoid")).MustInt64()) } if err != nil { log.Error(3, "Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err) ctx.Handle(500, "TeamsRepoAction", err) return } ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories") }
func SettingsSocial(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsSocial"] = true // Unbind social account. remove, _ := com.StrTo(ctx.Query("remove")).Int64() if remove > 0 { if err := models.DeleteOauth2ById(remove); err != nil { ctx.Handle(500, "DeleteOauth2ById", err) return } ctx.Flash.Success(ctx.Tr("settings.unbind_success")) ctx.Redirect(setting.AppSubUrl + "/user/settings/social") return } socials, err := models.GetOauthByUserId(ctx.User.Id) if err != nil { ctx.Handle(500, "GetOauthByUserId", err) return } ctx.Data["Socials"] = socials ctx.HTML(200, SETTINGS_SOCIAL) }
func Webhooks(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true // Delete web hook. remove := com.StrTo(ctx.Query("remove")).MustInt64() if remove > 0 { if err := models.DeleteWebhook(remove); err != nil { ctx.Handle(500, "DeleteWebhook", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success")) ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") return } ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id) if err != nil { ctx.Handle(500, "GetWebhooksByRepoId", err) return } ctx.Data["Webhooks"] = ws ctx.HTML(200, HOOKS) }
func Milestones(ctx *middleware.Context) { ctx.Data["Title"] = "Milestones" ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = true isShowClosed := ctx.Query("state") == "closed" miles, err := models.GetMilestones(ctx.Repo.Repository.Id, isShowClosed) if err != nil { ctx.Handle(500, "issue.Milestones(GetMilestones)", err) return } for _, m := range miles { m.RenderedContent = string(base.RenderMarkdown([]byte(m.Content), ctx.Repo.RepoLink)) m.CalOpenIssues() } ctx.Data["Milestones"] = miles if isShowClosed { ctx.Data["State"] = "closed" } else { ctx.Data["State"] = "open" } ctx.HTML(200, MILESTONE) }
func SearchUsers(ctx *middleware.Context) { opt := models.SearchOption{ Keyword: ctx.Query("q"), Limit: com.StrTo(ctx.Query("limit")).MustInt(), } if opt.Limit == 0 { opt.Limit = 10 } us, err := models.SearchUserByName(opt) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results := make([]*sdk.User, len(us)) for i := range us { results[i] = &sdk.User{ UserName: us[i].Name, AvatarUrl: us[i].AvatarLink(), FullName: us[i].FullName, } } ctx.Render.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) }
// FIXME: split to two different functions and pages to handle access token and oauth2 func SettingsApplicationsPost(ctx *middleware.Context, form auth.NewAccessTokenForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsApplications"] = true switch ctx.Query("type") { case "token": if ctx.HasError() { 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 MembersAction(ctx *middleware.Context) { uid := com.StrTo(ctx.Query("uid")).MustInt64() if uid == 0 { ctx.Redirect(ctx.Org.OrgLink + "/members") return } org := ctx.Org.Organization var err error switch ctx.Params(":action") { case "private": if ctx.User.Id != uid && !ctx.Org.IsOwner { ctx.Error(404) return } err = models.ChangeOrgUserStatus(org.Id, uid, false) case "public": if ctx.User.Id != uid { ctx.Error(404) return } err = models.ChangeOrgUserStatus(org.Id, uid, true) case "remove": if !ctx.Org.IsOwner { ctx.Error(404) return } err = org.RemoveMember(uid) if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.Redirect(ctx.Org.OrgLink + "/members") return } case "leave": err = org.RemoveMember(ctx.User.Id) if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.Redirect(ctx.Org.OrgLink + "/members") return } } if err != nil { log.Error(4, "Action(%s): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } if ctx.Params(":action") != "leave" { ctx.Redirect(ctx.Org.OrgLink + "/members") } else { ctx.Redirect(setting.AppSubUrl + "/") } }
func ResetPasswd(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("auth.reset_password") code := ctx.Query("code") if len(code) == 0 { ctx.Error(404) return } ctx.Data["Code"] = code ctx.Data["IsResetForm"] = true ctx.HTML(200, RESET_PASSWORD) }
func Email2User(ctx *middleware.Context) { u, err := models.GetUserByEmail(ctx.Query("email")) if err != nil { if err == models.ErrUserNotExist { ctx.Handle(404, "user.Email2User(GetUserByEmail)", err) } else { ctx.Handle(500, "user.Email2User(GetUserByEmail)", err) } return } ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name) }
func Commits(ctx *middleware.Context) { ctx.Data["IsRepoToolbarCommits"] = true userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name brs, err := ctx.Repo.GitRepo.GetBranches() if err != nil { ctx.Handle(500, "GetBranches", err) return } else if len(brs) == 0 { ctx.Handle(404, "GetBranches", nil) return } commitsCount, err := ctx.Repo.Commit.CommitsCount() if err != nil { ctx.Handle(500, "GetCommitsCount", err) return } // Calculate and validate page number. page, _ := com.StrTo(ctx.Query("p")).Int() if page < 1 { page = 1 } lastPage := page - 1 if lastPage < 0 { lastPage = 0 } nextPage := page + 1 if page*50 > commitsCount { nextPage = 0 } // Both `git log branchName` and `git log commitId` work. commits, err := ctx.Repo.Commit.CommitsByRange(page) if err != nil { ctx.Handle(500, "CommitsByRange", err) return } commits = RenderIssueLinks(commits, ctx.Repo.RepoLink) commits = models.ValidateCommitsWithEmails(commits) ctx.Data["Commits"] = commits ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName ctx.Data["CommitCount"] = commitsCount ctx.Data["LastPageNum"] = lastPage ctx.Data["NextPageNum"] = nextPage ctx.HTML(200, COMMITS) }
func Activate(ctx *middleware.Context) { code := ctx.Query("code") if len(code) == 0 { ctx.Data["IsActivatePage"] = true if ctx.User.IsActive { ctx.Error(404) return } // Resend confirmation e-mail. if setting.Service.RegisterEmailConfirm { if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { ctx.Data["ResendLimited"] = true } else { ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 mailer.SendActiveMail(ctx.Render, ctx.User) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } } } else { ctx.Data["ServiceNotEnabled"] = true } ctx.HTML(200, ACTIVATE) return } // Verify code. if user := models.VerifyUserActiveCode(code); user != nil { user.IsActive = true user.Rands = models.GetUserSalt() if err := models.UpdateUser(user); err != nil { if err == models.ErrUserNotExist { ctx.Error(404) } else { ctx.Handle(500, "UpdateUser", err) } return } log.Trace("User activated: %s", user.Name) ctx.Session.Set("uid", user.Id) ctx.Session.Set("uname", user.Name) ctx.Redirect(setting.AppSubUrl + "/") return } ctx.Data["IsActivateFailed"] = true ctx.HTML(200, ACTIVATE) }
func Dashboard(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("admin.dashboard") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminDashboard"] = true // Run operation. op, _ := com.StrTo(ctx.Query("op")).Int() if op > 0 { var err error var success string switch AdminOperation(op) { case CLEAN_UNBIND_OAUTH: success = ctx.Tr("admin.dashboard.clean_unbind_oauth_success") err = models.CleanUnbindOauth() case CLEAN_INACTIVATE_USER: success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success") err = models.DeleteInactivateUsers() case CLEAN_REPO_ARCHIVES: success = ctx.Tr("admin.dashboard.delete_repo_archives_success") err = models.DeleteRepositoryArchives() case GIT_GC_REPOS: success = ctx.Tr("admin.dashboard.git_gc_repos_success") err = models.GitGcRepos() case SYNC_SSH_AUTHORIZED_KEY: success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success") err = models.RewriteAllPublicKeys() case SYNC_REPOSITORY_UPDATE_HOOK: success = ctx.Tr("admin.dashboard.resync_all_update_hooks_success") err = models.RewriteRepositoryUpdateHook() } if err != nil { ctx.Flash.Error(err.Error()) } else { ctx.Flash.Success(success) } ctx.Redirect(setting.AppSubUrl + "/admin") return } ctx.Data["Stats"] = models.GetStatistic() // FIXME: update periodically updateSystemStatus() ctx.Data["SysStatus"] = sysStatus ctx.HTML(200, DASHBOARD) }
func ActivateEmail(ctx *middleware.Context) { code := ctx.Query("code") email_string := ctx.Query("email") // Verify code. if email := models.VerifyActiveEmailCode(code, email_string); email != nil { if err := email.Activate(); err != nil { ctx.Handle(500, "ActivateEmail", err) } log.Trace("Email activated: %s", email.Email) ctx.Flash.Success(ctx.Tr("settings.activate_email_success")) } ctx.Redirect(setting.AppSubUrl + "/user/settings/email") return }
func UpdateIssueMilestone(ctx *middleware.Context) { if !ctx.Repo.IsOwner() { ctx.Error(403) return } issueId := com.StrTo(ctx.Query("issue")).MustInt64() if issueId == 0 { ctx.Error(404) return } issue, err := models.GetIssueById(issueId) if err != nil { if err == models.ErrIssueNotExist { ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err) } else { ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err) } return } oldMid := issue.MilestoneId mid := com.StrTo(ctx.Query("milestoneid")).MustInt64() if oldMid == mid { ctx.JSON(200, map[string]interface{}{ "ok": true, }) return } // Not check for invalid milestone id and give responsibility to owners. issue.MilestoneId = mid if err = models.ChangeMilestoneAssign(oldMid, mid, issue); err != nil { ctx.Handle(500, "issue.UpdateIssueMilestone(ChangeMilestoneAssign)", err) return } else if err = models.UpdateIssue(issue); err != nil { ctx.Handle(500, "issue.UpdateIssueMilestone(UpdateIssue)", err) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func GitHooksEditPost(ctx *middleware.Context) { name := ctx.Params(":name") hook, err := ctx.Repo.GitRepo.GetHook(name) if err != nil { if err == git.ErrNotValidHook { ctx.Handle(404, "GetHook", err) } else { ctx.Handle(500, "GetHook", err) } return } hook.Content = ctx.Query("content") if err = hook.Update(); err != nil { ctx.Handle(500, "hook.Update", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git") }
func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) { idx := com.StrTo(ctx.Params(":index")).MustInt64() if idx <= 0 { ctx.Error(404) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx) if err != nil { if err == models.ErrIssueNotExist { ctx.Handle(404, "issue.UpdateIssue", err) } else { ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err) } return } if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner() { ctx.Error(403) return } issue.Name = form.IssueName //issue.MilestoneId = form.MilestoneId //issue.AssigneeId = form.AssigneeId //issue.LabelIds = form.Labels issue.Content = form.Content // try get content from text, ignore conflict with preview ajax if form.Content == "" { issue.Content = ctx.Query("text") } if err = models.UpdateIssue(issue); err != nil { ctx.Handle(500, "issue.UpdateIssue(UpdateIssue)", err) return } ctx.JSON(200, map[string]interface{}{ "ok": true, "title": issue.Name, "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)), }) }
func DeleteLabel(ctx *middleware.Context) { removes := ctx.Query("remove") if len(strings.TrimSpace(removes)) == 0 { ctx.JSON(200, map[string]interface{}{ "ok": true, }) return } strIds := strings.Split(removes, ",") for _, strId := range strIds { if err := models.DeleteLabel(ctx.Repo.Repository.Id, strId); err != nil { ctx.Handle(500, "issue.DeleteLabel(DeleteLabel)", err) return } } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func SearchRepos(ctx *middleware.Context) { opt := models.SearchOption{ Keyword: path.Base(ctx.Query("q")), Uid: com.StrTo(ctx.Query("uid")).MustInt64(), Limit: com.StrTo(ctx.Query("limit")).MustInt(), } if opt.Limit == 0 { opt.Limit = 10 } // Check visibility. if ctx.IsSigned && opt.Uid > 0 { if ctx.User.Id == opt.Uid { opt.Private = true } else { u, err := models.GetUserById(opt.Uid) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) { opt.Private = true } // FIXME: how about collaborators? } } repos, err := models.SearchRepositoryByName(opt) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results := make([]*sdk.Repository, len(repos)) for i := range repos { if err = repos[i].GetOwner(); err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } results[i] = &sdk.Repository{ Id: repos[i].Id, FullName: path.Join(repos[i].Owner.Name, repos[i].Name), } } ctx.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) }
func SearchCommits(ctx *middleware.Context) { ctx.Data["IsSearchPage"] = true ctx.Data["IsRepoToolbarCommits"] = true keyword := ctx.Query("q") if len(keyword) == 0 { ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) return } userName := ctx.Params(":username") repoName := ctx.Params(":reponame") brs, err := ctx.Repo.GitRepo.GetBranches() if err != nil { ctx.Handle(500, "GetBranches", err) return } else if len(brs) == 0 { ctx.Handle(404, "GetBranches", nil) return } commits, err := ctx.Repo.Commit.SearchCommits(keyword) if err != nil { ctx.Handle(500, "SearchCommits", err) return } commits = RenderIssueLinks(commits, ctx.Repo.RepoLink) commits = models.ValidateCommitsWithEmails(commits) ctx.Data["Keyword"] = keyword ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName ctx.Data["CommitCount"] = commits.Len() ctx.Data["Commits"] = commits ctx.HTML(200, COMMITS) }
func ResetPasswdPost(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("auth.reset_password") code := ctx.Query("code") if len(code) == 0 { ctx.Error(404) return } ctx.Data["Code"] = code if u := models.VerifyUserActiveCode(code); u != nil { // Validate password length. passwd := ctx.Query("password") if len(passwd) < 6 { ctx.Data["IsResetForm"] = true ctx.Data["Err_Password"] = true ctx.RenderWithErr(ctx.Tr("auth.password_too_short"), RESET_PASSWORD, nil) return } u.Passwd = passwd u.Rands = models.GetUserSalt() u.Salt = models.GetUserSalt() u.EncodePasswd() if err := models.UpdateUser(u); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User password reset: %s", u.Name) ctx.Redirect(setting.AppSubUrl + "/user/login") return } ctx.Data["IsResetFailed"] = true ctx.HTML(200, RESET_PASSWORD) }
func ForgotPasswdPost(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("auth.forgot_password") if setting.MailService == nil { ctx.Handle(403, "user.ForgotPasswdPost", nil) return } ctx.Data["IsResetRequest"] = true email := ctx.Query("email") u, err := models.GetUserByEmail(email) if err != nil { if err == models.ErrUserNotExist { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("auth.email_not_associate"), FORGOT_PASSWORD, nil) } else { ctx.Handle(500, "user.ResetPasswd(check existence)", err) } return } if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { ctx.Data["ResendLimited"] = true ctx.HTML(200, FORGOT_PASSWORD) return } mailer.SendResetPasswdMail(ctx.Render, u) if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } ctx.Data["Email"] = email ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 ctx.Data["IsResetSent"] = true ctx.HTML(200, FORGOT_PASSWORD) }
func UpdateAssignee(ctx *middleware.Context) { if !ctx.Repo.IsOwner() { ctx.Error(403) return } issueId := com.StrTo(ctx.Query("issue")).MustInt64() if issueId == 0 { ctx.Error(404) return } issue, err := models.GetIssueById(issueId) if err != nil { if err == models.ErrIssueNotExist { ctx.Handle(404, "GetIssueById", err) } else { ctx.Handle(500, "GetIssueById", err) } return } aid := com.StrTo(ctx.Query("assigneeid")).MustInt64() // Not check for invalid assignee id and give responsibility to owners. issue.AssigneeId = aid if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil { ctx.Handle(500, "UpdateIssueUserPairByAssignee: %v", err) return } else if err = models.UpdateIssue(issue); err != nil { ctx.Handle(500, "UpdateIssue", err) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func UpdateLabel(ctx *middleware.Context, form auth.CreateLabelForm) { id := com.StrTo(ctx.Query("id")).MustInt64() if id == 0 { ctx.Error(404) return } l, err := models.GetLabelById(id) if l == nil { log.Warn("Could not find label id in db: %s", err) ctx.Redirect(ctx.Repo.RepoLink + "/issues") return } l.Name = form.Title l.Color = form.Color if err := models.UpdateLabel(l); err != nil { ctx.Handle(500, "issue.UpdateLabel(UpdateLabel)", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/issues") }
func Action(ctx *middleware.Context) { var err error switch ctx.Params(":action") { case "watch": err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true) case "unwatch": err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false) case "star": err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, true) case "unstar": err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, false) case "desc": if !ctx.Repo.IsOwner() { ctx.Error(404) return } ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") err = models.UpdateRepository(ctx.Repo.Repository, false) } if err != nil { log.Error(4, "Action(%s): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } ctx.Redirect(ctx.Repo.RepoLink) return ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func TeamsAction(ctx *middleware.Context) { uid := com.StrTo(ctx.Query("uid")).MustInt64() if uid == 0 { ctx.Redirect(ctx.Org.OrgLink + "/teams") return } page := ctx.Query("page") var err error switch ctx.Params(":action") { case "join": if !ctx.Org.IsOwner { ctx.Error(404) return } err = ctx.Org.Team.AddMember(ctx.User.Id) case "leave": err = ctx.Org.Team.RemoveMember(ctx.User.Id) case "remove": if !ctx.Org.IsOwner { ctx.Error(404) return } err = ctx.Org.Team.RemoveMember(uid) page = "team" case "add": if !ctx.Org.IsOwner { ctx.Error(404) return } uname := ctx.Query("uname") var u *models.User u, err = models.GetUserByName(uname) if err != nil { if err == models.ErrUserNotExist { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName) } else { ctx.Handle(500, " GetUserByName", err) } return } err = ctx.Org.Team.AddMember(u.Id) page = "team" } if err != nil { if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) } else { log.Error(3, "Action(%s): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } } switch page { case "team": ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName) default: ctx.Redirect(ctx.Org.OrgLink + "/teams") } }
func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsEmails"] = true emails, err := models.GetEmailAddresses(ctx.User.Id) if err != nil { ctx.Handle(500, "GetEmailAddresses", err) return } ctx.Data["Emails"] = emails // Delete E-mail address. if ctx.Query("_method") == "DELETE" { id := ctx.QueryInt64("id") if id <= 0 { return } if err = models.DeleteEmailAddress(&models.EmailAddress{Id: id}); err != nil { ctx.Handle(500, "DeleteEmail", err) } else { log.Trace("Email address deleted: %s", ctx.User.Name) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") } return } // Make emailaddress primary. if ctx.Query("_method") == "PRIMARY" { id := ctx.QueryInt64("id") if id <= 0 { return } if err = models.MakeEmailPrimary(&models.EmailAddress{Id: id}); err != nil { ctx.Handle(500, "MakeEmailPrimary", err) } else { log.Trace("Email made primary: %s", ctx.User.Name) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") } return } // Add Email address. if ctx.HasError() { ctx.HTML(200, SETTINGS_EMAILS) return } cleanEmail := strings.Replace(form.Email, "\n", "", -1) e := &models.EmailAddress{ Uid: ctx.User.Id, Email: cleanEmail, IsActivated: !setting.Service.RegisterEmailConfirm, } if err := models.AddEmailAddress(e); err != nil { if err == models.ErrEmailAlreadyUsed { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form) return } ctx.Handle(500, "AddEmailAddress", err) return } else { // Send confirmation e-mail if setting.Service.RegisterEmailConfirm { mailer.SendActivateEmail(ctx.Render, ctx.User, e) 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", cleanEmail, setting.Service.ActiveCodeLives/60)) } else { ctx.Flash.Success(ctx.Tr("settings.add_email_success")) } log.Trace("Email address added: %s", e.Email) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") return } ctx.HTML(200, SETTINGS_EMAILS) }
func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsSSHKeys"] = true var err error ctx.Data["Keys"], err = models.ListPublicKeys(ctx.User.Id) if err != nil { ctx.Handle(500, "ssh.ListPublicKey", err) return } // Delete SSH key. if ctx.Query("_method") == "DELETE" { id := com.StrTo(ctx.Query("id")).MustInt64() if id <= 0 { return } if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil { ctx.Handle(500, "DeletePublicKey", err) } else { log.Trace("SSH key deleted: %s", ctx.User.Name) ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh") } return } // Add new SSH key. if ctx.Req.Method == "POST" { if ctx.HasError() { ctx.HTML(200, SETTINGS_SSH_KEYS) return } // Parse openssh style string from form content content, err := models.ParseKeyString(form.Content) if err != nil { ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh") return } if ok, err := models.CheckPublicKeyString(content); !ok { if err == models.ErrKeyUnableVerify { 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 } } k := &models.PublicKey{ OwnerId: ctx.User.Id, Name: form.SSHTitle, Content: content, } if err := models.AddPublicKey(k); err != nil { if err == models.ErrKeyAlreadyExist { ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form) return } ctx.Handle(500, "ssh.AddPublicKey", err) return } else { log.Trace("SSH key added: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.add_key_success")) ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh") return } } ctx.HTML(200, SETTINGS_SSH_KEYS) }
func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) { ctx.Data["Title"] = ctx.Tr("sign_up") if setting.Service.DisableRegistration { ctx.Error(403) return } isOauth := false sid, isOauth := ctx.Session.Get("socialId").(int64) if isOauth { ctx.Data["IsSocialLogin"] = true } // May redirect from home page. if ctx.Query("from") == "home" { // Clear input error box. ctx.Data["Err_UserName"] = false ctx.Data["Err_Email"] = false // Make the best guess. uname := ctx.Query("uname") i := strings.Index(uname, "@") if i > -1 { ctx.Data["email"] = uname ctx.Data["uname"] = uname[:i] } else { ctx.Data["uname"] = uname } ctx.Data["password"] = ctx.Query("password") ctx.HTML(200, SIGNUP) return } if ctx.HasError() { ctx.HTML(200, SIGNUP) return } if !cpt.VerifyReq(ctx.Req) { ctx.Data["Err_Captcha"] = true ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form) return } else if form.Password != form.Retype { ctx.Data["Err_Password"] = true ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form) return } u := &models.User{ Name: form.UserName, Email: form.Email, Passwd: form.Password, IsActive: !setting.Service.RegisterEmailConfirm || isOauth, } if err := models.CreateUser(u); err != nil { switch err { case models.ErrUserAlreadyExist: ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form) case models.ErrEmailAlreadyUsed: ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form) case models.ErrUserNameIllegal: ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SIGNUP, &form) default: ctx.Handle(500, "CreateUser", err) } return } log.Trace("Account created: %s", u.Name) // Bind social account. if isOauth { if err := models.BindUserOauth2(u.Id, sid); err != nil { ctx.Handle(500, "BindUserOauth2", err) return } ctx.Session.Delete("socialId") log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid) } // Send confirmation e-mail, no need for social account. if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 { mailer.SendRegisterMail(ctx.Render, u) ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 ctx.HTML(200, ACTIVATE) if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } return } ctx.Redirect(setting.AppSubUrl + "/user/login") }