Merge pull request #1 from reasonerjt/master

refinement
This commit is contained in:
reasonerjt 2016-02-17 14:31:51 +08:00
commit d1f90463e3
7 changed files with 46 additions and 45 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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

View File

@ -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{})

View File

@ -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)
}

View File

@ -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 || [];
}