From 90aef78b1b72edd879c2ffede10abe5a5a8ba5d3 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Wed, 17 Feb 2016 14:29:05 +0800 Subject: [PATCH] refinement --- Deploy/db/registry.sql | 3 +- api/project.go | 54 ++++++++++++++++-------------- dao/project.go | 2 +- models/access_log.go | 24 ++++++------- routers/router.go | 1 - service/utils/auth_utils.go | 1 - static/resources/js/item-detail.js | 6 ++-- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/Deploy/db/registry.sql b/Deploy/db/registry.sql index 4c9f03d28..741ab6348 100644 --- a/Deploy/db/registry.sql +++ b/Deploy/db/registry.sql @@ -60,7 +60,8 @@ create table project ( deleted tinyint (1) DEFAULT 0 NOT NULL, public tinyint (1) DEFAULT 0 NOT NULL, primary key (project_id), - FOREIGN KEY (owner_id) REFERENCES user(user_id) + FOREIGN KEY (owner_id) REFERENCES user(user_id), + UNIQUE (name) ); insert into project values diff --git a/api/project.go b/api/project.go index 9f92befa6..9c711e17d 100644 --- a/api/project.go +++ b/api/project.go @@ -38,6 +38,8 @@ type projectReq struct { Public bool `json:"public"` } +const PROJECT_NAME_MAX_LEN int = 30 + func (p *ProjectAPI) Prepare() { p.userId = p.ValidateUser() id_str := p.Ctx.Input.Param(":id") @@ -48,12 +50,12 @@ func (p *ProjectAPI) Prepare() { log.Printf("Error parsing project id: %s, error: %v", id_str, err) p.CustomAbort(400, "invalid project id") } - proj, err := dao.GetProjectById(models.Project{ProjectId: p.projectId}) + exist, err := dao.ProjectExists(p.projectId) if err != nil { - log.Printf("Error occurred in GetProjectById: %v", err) + log.Printf("Error occurred in ProjectExists: %v", err) p.CustomAbort(500, "Internal error.") } - if proj == nil { + if !exist { p.CustomAbort(404, fmt.Sprintf("project does not exist, id: %v", p.projectId)) } } @@ -66,6 +68,12 @@ func (p *ProjectAPI) Post() { if req.Public { public = 1 } + err := validateProjectReq(req) + if err != nil { + beego.Error("Invalid project request, error: ", err) + p.RenderError(400, "Invalid request for creating project") + return + } projectName := req.ProjectName exist, err := dao.ProjectExists(projectName) if err != nil { @@ -76,13 +84,18 @@ func (p *ProjectAPI) Post() { return } project := models.Project{OwnerId: p.userId, Name: projectName, CreationTime: time.Now(), Public: public} - dao.AddProject(project) + err = dao.AddProject(project) + if err != nil { + beego.Error("Failed to add project, error: %v", err) + p.RenderError(500, "Failed to add project") + } } func (p *ProjectAPI) Head() { projectName := p.GetString("project_name") result, err := dao.ProjectExists(projectName) if err != nil { + beego.Error("Error while communicating with DB: ", err) p.RenderError(500, "Error while communicating with DB") return } @@ -130,16 +143,6 @@ func (p *ProjectAPI) Put() { if req.Public { public = 1 } - - proj, err := dao.GetProjectById(models.Project{ProjectId: projectId}) - if err != nil { - beego.Error("Error occurred in GetProjectById:", err) - p.CustomAbort(500, "Internal error.") - } - if proj == nil { - p.RenderError(404, "") - return - } if !isProjectAdmin(p.userId, projectId) { beego.Warning("Current user, id:", p.userId, ", does not have project admin role for project, id:", projectId) p.RenderError(403, "") @@ -152,18 +155,6 @@ func (p *ProjectAPI) Put() { } } -func (p *ProjectAPI) ListAccessLog() { - - query := models.AccessLog{ProjectId: p.projectId} - accessLogList, err := dao.GetAccessLogs(query) - if err != nil { - log.Printf("Error occurred in GetAccessLogs: %v", err) - p.CustomAbort(500, "Internal error.") - } - p.Data["json"] = accessLogList - p.ServeJSON() -} - func (p *ProjectAPI) FilterAccessLog() { var filter models.AccessLog @@ -193,3 +184,14 @@ func isProjectAdmin(userId int, pid int64) bool { } return len(rolelist) > 0 } + +func validateProjectReq(req projectReq) error { + pn := req.ProjectName + if len(pn) == 0 { + return fmt.Errorf("Project name can not be empty") + } + if len(pn) > PROJECT_NAME_MAX_LEN { + return fmt.Errorf("Project name is too long") + } + return nil +} diff --git a/dao/project.go b/dao/project.go index f177bddc3..6f228e5e6 100644 --- a/dao/project.go +++ b/dao/project.go @@ -121,7 +121,7 @@ func QueryProject(query models.Project) ([]models.Project, error) { queryParam = append(queryParam, query.Name) } - sql += " order by p.creation_time desc " + sql += " order by p.name " var r []models.Project _, err := o.Raw(sql, queryParam).QueryRows(&r) diff --git a/models/access_log.go b/models/access_log.go index 72d15253e..7c980103d 100644 --- a/models/access_log.go +++ b/models/access_log.go @@ -1,16 +1,16 @@ /* - Copyright (c) 2016 VMware, Inc. All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Copyright (c) 2016 VMware, Inc. All Rights Reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. */ package models diff --git a/routers/router.go b/routers/router.go index 44082951e..e4b072054 100644 --- a/routers/router.go +++ b/routers/router.go @@ -53,7 +53,6 @@ func init() { beego.Router("/api/search", &api.SearchAPI{}) beego.Router("/api/projects/:pid/members/?:mid", &api.ProjectMemberAPI{}) beego.Router("/api/projects/?:id", &api.ProjectAPI{}) - beego.Router("/api/projects/:id/logs", &api.ProjectAPI{}, "get:ListAccessLog") beego.Router("/api/projects/:id/logs/filter", &api.ProjectAPI{}, "post:FilterAccessLog") beego.Router("/api/users/?:id", &api.UserAPI{}) beego.Router("/api/repositories", &api.RepositoryAPI{}) diff --git a/service/utils/auth_utils.go b/service/utils/auth_utils.go index 54e83047b..ce1cbb140 100644 --- a/service/utils/auth_utils.go +++ b/service/utils/auth_utils.go @@ -168,7 +168,6 @@ func makeTokenCore(issuer, subject, audience string, expiration int, signature := base64UrlEncode(signatureBytes) tokenString := fmt.Sprintf("%s.%s", payload, signature) - //log.Printf("token: %s", tokenString) return token.NewToken(tokenString) } diff --git a/static/resources/js/item-detail.js b/static/resources/js/item-detail.js index 53591b6ce..d2ff211bb 100644 --- a/static/resources/js/item-detail.js +++ b/static/resources/js/item-detail.js @@ -354,9 +354,9 @@ jQuery(function(){ $.when( new AjaxUtil({ - url : "/api/projects/" + projectId + "/logs", - dataRaw: {"timestamp": new Date().getTime()}, - type: "get", + url : "/api/projects/" + projectId + "/logs/filter", + data: {}, + type: "post", success: function(data){ return data || []; }