func checkContextUser(ctx *middleware.Context, uid int64) *models.User { orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.Id, "updated") if err != nil { ctx.Handle(500, "GetOwnedOrgsByUserIDDesc", err) return nil } ctx.Data["Orgs"] = orgs // Not equal means current user is an organization. if uid == ctx.User.Id || uid == 0 { return ctx.User } org, err := models.GetUserByID(uid) if models.IsErrUserNotExist(err) { return ctx.User } if err != nil { ctx.Handle(500, "GetUserByID", fmt.Errorf("[%d]: %v", uid, err)) return nil } // Check ownership of organization. if !org.IsOrganization() || !(ctx.User.IsAdmin || org.IsOwnedBy(ctx.User.Id)) { ctx.Error(403) return nil } return org }
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 checkContextUser(ctx *middleware.Context, uid int64) *models.User { if err := ctx.User.GetOrganizations(); err != nil { ctx.Handle(500, "GetOrganizations", err) return nil } ctx.Data["Orgs"] = ctx.User.Orgs // Not equal means current user is an organization. if uid == ctx.User.Id || uid == 0 { return ctx.User } org, err := models.GetUserByID(uid) if models.IsErrUserNotExist(err) { return ctx.User } if err != nil { ctx.Handle(500, "checkContextUser", fmt.Errorf("GetUserById(%d): %v", uid, err)) return nil } // Check ownership of organization. if !org.IsOrganization() || !org.IsOwnedBy(ctx.User.Id) { ctx.Error(403) return nil } return org }
func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_repo") ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctxUser := ctx.User // Not equal means current user is an organization. if form.Uid != ctx.User.Id { var err error ctxUser, err = checkContextUser(ctx, form.Uid) if err != nil { ctx.Handle(500, "checkContextUser", err) return } } ctx.Data["ContextUser"] = ctxUser if err := ctx.User.GetOrganizations(); err != nil { ctx.Handle(500, "GetOrganizations", err) return } ctx.Data["Orgs"] = ctx.User.Orgs if ctx.HasError() { ctx.HTML(200, CREATE) return } if ctxUser.IsOrganization() { // Check ownership of organization. if !ctxUser.IsOwnedBy(ctx.User.Id) { ctx.Error(403) return } } repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description, form.Gitignore, form.License, form.Private, false, form.AutoInit) if err == nil { log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } else if err == models.ErrRepoAlreadyExist { ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) return } else if err == models.ErrRepoNameIllegal { ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form) return } if repo != nil { if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } ctx.Handle(500, "CreatePost", err) }
func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) { repo, err := models.CreateRepository(owner, models.CreateRepoOptions{ Name: opt.Name, Description: opt.Description, Gitignores: opt.Gitignores, License: opt.License, Readme: opt.Readme, IsPrivate: opt.Private, AutoInit: opt.AutoInit, }) if err != nil { if models.IsErrRepoAlreadyExist(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL}) } else { log.Error(4, "CreateRepository: %v", err) if repo != nil { if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil { log.Error(4, "DeleteRepository: %v", err) } } ctx.Error(500) } return } ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true})) }
func Download(ctx *middleware.Context) { ext := "." + ctx.Params(":ext") var archivePath string switch ext { case ".zip": archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") case ".tar.gz": archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") default: ctx.Error(404) return } if !com.IsDir(archivePath) { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext) if !com.IsFile(archivePath) { if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext) }
func Action(ctx *middleware.Context, params martini.Params) { var err error switch 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 "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) } if err != nil { log.Error("repo.Action(%s): %v", params["action"], err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func SettingPost(ctx *middleware.Context, params martini.Params) { if !ctx.Repo.IsOwner { ctx.Error(404) return } switch ctx.Query("action") { case "update": ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { ctx.Handle(404, "repo.SettingPost(update)", err) return } ctx.Data["IsSuccess"] = true ctx.HTML(200, "repo/setting") log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.LowerName) case "delete": if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { ctx.Data["ErrorMsg"] = "Please make sure you entered repository name is correct." ctx.HTML(200, "repo/setting") return } if err := models.DeleteRepository(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.LowerName); err != nil { ctx.Handle(200, "repo.Delete", err) return } log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.LowerName) ctx.Redirect("/") } }
func checkUserFollowing(ctx *middleware.Context, u *models.User, followID int64) { if u.IsFollowing(followID) { ctx.Status(204) } else { ctx.Error(404) } }
func Profile(ctx *middleware.Context) { ctx.Data["Title"] = "Profile" ctx.Data["PageIsUserProfile"] = true uname := ctx.Params(":username") // Special handle for FireFox requests favicon.ico. if uname == "favicon.ico" { ctx.Redirect(setting.AppSubUrl + "/img/favicon.png") return } else if strings.HasSuffix(uname, ".png") { ctx.Error(404) return } isShowKeys := false if strings.HasSuffix(uname, ".keys") { isShowKeys = true uname = strings.TrimSuffix(uname, ".keys") } u, err := models.GetUserByName(uname) if err != nil { if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", err) } else { ctx.Handle(500, "GetUserByName", err) } return } // Show SSH keys. if isShowKeys { ShowSSHKeys(ctx, u.Id) return } if u.IsOrganization() { showOrgProfile(ctx) return } ctx.Data["Owner"] = u tab := ctx.Query("tab") ctx.Data["TabName"] = tab switch tab { case "activity": retrieveFeeds(ctx, u.Id, 0, true) if ctx.Written() { return } default: ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id) if err != nil { ctx.Handle(500, "GetRepositories", err) return } } ctx.HTML(200, PROFILE) }
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": // FIXME: this is not used 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 { ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err) return } redirectTo := ctx.Query("redirect_to") if len(redirectTo) == 0 { redirectTo = ctx.Repo.RepoLink } ctx.Redirect(redirectTo) }
func DeleteRepo(ctx *middleware.Context) { user, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.APIError(422, "", err) } else { ctx.APIError(500, "GetUserByName", err) } return } repo, err := models.GetRepositoryByName(user.Id, ctx.Params(":reponame")) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Error(404) } else { ctx.APIError(500, "GetRepositoryByName", err) } return } if user.IsOrganization() && !user.IsOwnedBy(ctx.User.Id) { ctx.APIError(403, "", "Given user is not owner of organization.") return } if err := models.DeleteRepository(user.Id, repo.ID); err != nil { ctx.APIError(500, "DeleteRepository", err) return } log.Trace("Repository deleted: %s/%s", user.Name, repo.Name) ctx.Status(204) }
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 { 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): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories") }
func UpdateIssueTitle(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } if !ctx.IsSigned || (ctx.User.Id != issue.PosterID && !ctx.Repo.IsAdmin()) { ctx.Error(403) return } issue.Name = ctx.QueryTrim("title") if len(issue.Name) == 0 { ctx.Error(204) return } if err := models.UpdateIssue(issue); err != nil { ctx.Handle(500, "UpdateIssue", err) return } ctx.JSON(200, map[string]interface{}{ "title": issue.Name, }) }
func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = ctx.Tr("admin.auths.edit") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["PageIsAuths"] = true ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, AUTH_EDIT) return } var config core.Conversion switch models.LoginType(form.Type) { case models.LDAP: config = &models.LDAPConfig{ Ldapsource: ldap.Ldapsource{ Host: form.Host, Port: form.Port, UseSSL: form.UseSSL, BaseDN: form.BaseDN, Attributes: form.Attributes, Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, Name: form.AuthName, }, } case models.SMTP: config = &models.SMTPConfig{ Auth: form.SmtpAuth, Host: form.SmtpHost, Port: form.SmtpPort, TLS: form.Tls, } default: ctx.Error(400) return } u := models.LoginSource{ Id: form.Id, Name: form.AuthName, IsActived: form.IsActived, Type: models.LoginType(form.Type), AllowAutoRegister: form.AllowAutoRegister, Cfg: config, } if err := models.UpdateSource(&u); err != nil { ctx.Handle(500, "UpdateSource", err) return } log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, form.AuthName) ctx.Flash.Success(ctx.Tr("admin.auths.update_success")) ctx.Redirect("/admin/auths/" + ctx.Params(":authid")) }
func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_repo") ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if err := ctx.User.GetOrganizations(); err != nil { ctx.Handle(500, "GetOrganizations", err) return } ctx.Data["Orgs"] = ctx.User.Orgs if ctx.HasError() { ctx.HTML(200, CREATE) return } if ctxUser.IsOrganization() { // Check ownership of organization. if !ctxUser.IsOwnedBy(ctx.User.Id) { ctx.Error(403) return } } repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description, form.Gitignore, form.License, form.Private, false, form.AutoInit) if err == nil { log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } if repo != nil { if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } switch { case err == models.ErrRepoAlreadyExist: ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) case models.IsErrNameReserved(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form) case models.IsErrNamePatternNotAllowed(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form) default: ctx.Handle(500, "CreatePost", err) } }
func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = ctx.Tr("admin.auths.new") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, AUTH_NEW) return } var u core.Conversion switch models.LoginType(form.Type) { case models.LDAP: u = &models.LDAPConfig{ Ldapsource: ldap.Ldapsource{ Host: form.Host, Port: form.Port, UseSSL: form.UseSSL, BaseDN: form.BaseDN, AttributeUsername: form.AttributeUsername, AttributeName: form.AttributeName, AttributeSurname: form.AttributeSurname, AttributeMail: form.AttributeMail, Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, Name: form.Name, }, } case models.SMTP: u = &models.SMTPConfig{ Auth: form.SMTPAuth, Host: form.SMTPHost, Port: form.SMTPPort, TLS: form.TLS, } default: ctx.Error(400) return } var source = &models.LoginSource{ Type: models.LoginType(form.Type), Name: form.Name, IsActived: true, AllowAutoRegister: form.AllowAutoRegister, Cfg: u, } if err := models.CreateSource(source); err != nil { ctx.Handle(500, "CreateSource", err) return } log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name) ctx.Redirect(setting.AppSubUrl + "/admin/auths") }
func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = "Create repository" ctx.Data["PageIsNewRepo"] = true ctx.Data["LanguageIgns"] = models.LanguageIgns ctx.Data["Licenses"] = models.Licenses if err := ctx.User.GetOrganizations(); err != nil { ctx.Handle(500, "home.CreatePost(GetOrganizations)", err) return } ctx.Data["Orgs"] = ctx.User.Orgs if ctx.HasError() { ctx.HTML(200, CREATE) return } u := ctx.User // Not equal means current user is an organization. if u.Id != form.Uid { var err error u, err = models.GetUserById(form.Uid) if err != nil { if err == models.ErrUserNotExist { ctx.Handle(404, "home.CreatePost(GetUserById)", err) } else { ctx.Handle(500, "home.CreatePost(GetUserById)", err) } return } // Check ownership of organization. if !u.IsOrgOwner(ctx.User.Id) { ctx.Error(403) return } } repo, err := models.CreateRepository(u, form.RepoName, form.Description, form.Language, form.License, form.Private, false, form.InitReadme) if err == nil { log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName) ctx.Redirect("/" + u.Name + "/" + form.RepoName) return } else if err == models.ErrRepoAlreadyExist { ctx.RenderWithErr("Repository name has already been used", CREATE, &form) return } else if err == models.ErrRepoNameIllegal { ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), CREATE, &form) return } if repo != nil { if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { log.Error("repo.CreatePost(DeleteRepository): %v", errDelete) } } ctx.Handle(500, "repo.CreatePost(CreateRepository)", err) }
func SettingPost(ctx *middleware.Context) { if !ctx.Repo.IsOwner { ctx.Error(404) return } switch ctx.Query("action") { case "update": isNameChanged := false newRepoName := ctx.Query("name") // Check if repository name has been changed. if ctx.Repo.Repository.Name != newRepoName { isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) if err != nil { ctx.Handle(404, "repo.SettingPost(update: check existence)", err) return } else if isExist { ctx.RenderWithErr("Repository name has been taken in your repositories.", "repo/setting", nil) return } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil { ctx.Handle(404, "repo.SettingPost(change repository name)", err) return } log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName) isNameChanged = true ctx.Repo.Repository.Name = newRepoName } ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { ctx.Handle(404, "repo.SettingPost(update)", err) return } ctx.Data["IsSuccess"] = true if isNameChanged { ctx.Redirect(fmt.Sprintf("/%s/%s/settings", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)) } else { ctx.HTML(200, "repo/setting") } log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) case "delete": if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { ctx.RenderWithErr("Please make sure you entered repository name is correct.", "repo/setting", nil) return } if err := models.DeleteRepository(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.LowerName); err != nil { ctx.Handle(200, "repo.Delete", err) return } log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.LowerName) ctx.Redirect("/") } }
func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = "Edit Authentication" ctx.Data["PageIsAuths"] = true ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, "admin/auths/edit") return } var config core.Conversion switch form.Type { case models.LT_LDAP: config = &models.LDAPConfig{ Ldapsource: ldap.Ldapsource{ Host: form.Host, Port: form.Port, UseSSL: form.UseSSL, BaseDN: form.BaseDN, Attributes: form.Attributes, Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, Name: form.AuthName, }, } case models.LT_SMTP: config = &models.SMTPConfig{ Auth: form.SmtpAuth, Host: form.SmtpHost, Port: form.SmtpPort, TLS: form.Tls, } default: ctx.Error(400) return } u := models.LoginSource{ Id: form.Id, Name: form.AuthName, IsActived: form.IsActived, Type: form.Type, AllowAutoRegister: form.AllowAutoRegister, Cfg: config, } if err := models.UpdateSource(&u); err != nil { ctx.Handle(500, "admin.auths.EditAuth", err) return } log.Trace("%s Authentication changed by admin(%s): %s", ctx.Req.RequestURI, ctx.User.LowerName, form.AuthName) ctx.Redirect("/admin/auths") }
func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { ctx.Data["Title"] = "New Authentication" ctx.Data["PageIsAuths"] = true ctx.Data["LoginTypes"] = models.LoginTypes ctx.Data["SMTPAuths"] = models.SMTPAuths if ctx.HasError() { ctx.HTML(200, AUTH_NEW) return } var u core.Conversion switch models.LoginType(form.Type) { case models.LDAP: u = &models.LDAPConfig{ Ldapsource: ldap.Ldapsource{ Host: form.Host, Port: form.Port, UseSSL: form.UseSSL, BaseDN: form.BaseDN, Attributes: form.Attributes, Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, Name: form.AuthName, }, } case models.SMTP: u = &models.SMTPConfig{ Auth: form.SmtpAuth, Host: form.SmtpHost, Port: form.SmtpPort, TLS: form.Tls, } default: ctx.Error(400) return } var source = &models.LoginSource{ Type: models.LoginType(form.Type), Name: form.AuthName, IsActived: true, AllowAutoRegister: form.AllowAutoRegister, Cfg: u, } if err := models.CreateSource(source); err != nil { ctx.Handle(500, "admin.auths.NewAuth(CreateSource)", err) return } log.Trace("%s Authentication created by admin(%s): %s", ctx.Req.RequestURI, ctx.User.LowerName, strings.ToLower(form.AuthName)) ctx.Redirect("/admin/auths") }
func NewTeamPost(ctx *middleware.Context, params martini.Params, form auth.CreateTeamForm) { org, err := models.GetUserByName(params["org"]) if err != nil { if err == models.ErrUserNotExist { ctx.Handle(404, "org.NewTeamPost(GetUserByName)", err) } else { ctx.Handle(500, "org.NewTeamPost(GetUserByName)", err) } return } ctx.Data["Org"] = org // Check ownership of organization. if !org.IsOrgOwner(ctx.User.Id) { ctx.Error(403) return } if ctx.HasError() { ctx.HTML(200, TEAM_NEW) return } // Validate permission level. var auth models.AuthorizeType switch form.Permission { case "read": auth = models.ORG_READABLE case "write": auth = models.ORG_WRITABLE case "admin": auth = models.ORG_ADMIN default: ctx.Error(401) return } t := &models.Team{ OrgId: org.Id, Name: form.TeamName, Description: form.Description, Authorize: auth, } if err = models.NewTeam(t); err != nil { if err == models.ErrTeamAlreadyExist { ctx.Data["Err_TeamName"] = true ctx.RenderWithErr("Team name has already been used", TEAM_NEW, &form) } else { ctx.Handle(500, "org.NewTeamPost(NewTeam)", err) } return } log.Trace("%s Team created: %s/%s", ctx.Req.RequestURI, org.Name, t.Name) ctx.Redirect("/org/" + org.LowerName + "/teams/" + t.LowerName) }
func Profile(ctx *middleware.Context) { uname := ctx.Params(":username") // Special handle for FireFox requests favicon.ico. if uname == "favicon.ico" { ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png")) return } else if strings.HasSuffix(uname, ".png") { ctx.Error(404) return } isShowKeys := false if strings.HasSuffix(uname, ".keys") { isShowKeys = true } u := GetUserByParams(ctx) if ctx.Written() { return } // Show SSH keys. if isShowKeys { ShowSSHKeys(ctx, u.Id) return } if u.IsOrganization() { showOrgProfile(ctx) return } ctx.Data["Title"] = u.DisplayName() ctx.Data["PageIsUserProfile"] = true ctx.Data["Owner"] = u tab := ctx.Query("tab") ctx.Data["TabName"] = tab switch tab { case "activity": retrieveFeeds(ctx, u.Id, 0, true) if ctx.Written() { return } default: var err error ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id) if err != nil { ctx.Handle(500, "GetRepositories", err) return } } ctx.HTML(200, PROFILE) }
func ForkPost(ctx *middleware.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 { switch { case models.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form) case models.IsErrNameReserved(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) case models.IsErrNamePatternNotAllowed(err): ctx.Data["Err_RepoName"] = true 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 GetUserByName(ctx *middleware.Context, name string) *models.User { user, err := models.GetUserByName(name) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(404) } else { ctx.Handle(500, "GetUserByName", err) } return nil } return user }
func getActionIssue(ctx *middleware.Context) *models.Issue { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Error(404, "GetIssueByIndex") } else { ctx.Handle(500, "GetIssueByIndex", err) } return nil } return issue }
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user func ListUserPublicKeys(ctx *middleware.Context) { user, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(404) } else { ctx.APIError(500, "GetUserByName", err) } return } listUserPublicKeys(ctx, user.Id) }
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) }
// GET /users/:username func GetUserInfo(ctx *middleware.Context) { u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if err == models.ErrUserNotExist { ctx.Error(404) } else { ctx.JSON(500, &base.ApiJsonErr{"GetUserByName: " + err.Error(), base.DOC_URL}) } return } ctx.JSON(200, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()}) }
func ResetPasswd(ctx *middleware.Context) { ctx.Data["Title"] = "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, "user/reset_passwd") }