MCPcopy
hub / github.com/eyebluecn/tank / CreateAdmin

Method CreateAdmin

code/rest/install_controller.go:350–409  ·  view source on GitHub ↗

create admin

(writer http.ResponseWriter, request *http.Request)

Source from the content-addressed store, hash-verified

348
349// create admin
350func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *http.Request) *result.WebResult {
351
352 db := this.openDbConnection(writer, request)
353 defer this.closeDbConnection(db)
354
355 adminUsername := request.FormValue("adminUsername")
356 adminPassword := request.FormValue("adminPassword")
357
358 //validate admin's username
359 if m, _ := regexp.MatchString(USERNAME_PATTERN, adminUsername); !m {
360 panic(result.BadRequestI18n(request, i18n.UsernameError))
361 }
362
363 if len(adminPassword) < 6 {
364 panic(result.BadRequest(`admin's password at least 6 chars'`))
365 }
366
367 //check whether duplicate
368 var count2 int64
369 db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2)
370 this.PanicError(db2.Error)
371 if count2 > 0 {
372 panic(result.BadRequestI18n(request, i18n.UsernameExist, adminUsername))
373 }
374
375 space := &Space{}
376 timeUUID, _ := uuid.NewV4()
377 space.Uuid = timeUUID.String()
378 space.CreateTime = time.Now()
379 space.UpdateTime = time.Now()
380 space.Name = adminUsername
381 space.UserUuid = ""
382 space.SizeLimit = -1
383 space.Type = SPACE_TYPE_PRIVATE
384 db3 := db.Create(space)
385 this.PanicError(db3.Error)
386
387 user := &User{}
388 timeUUID, _ = uuid.NewV4()
389 user.Uuid = timeUUID.String()
390 user.CreateTime = time.Now()
391 user.UpdateTime = time.Now()
392 user.LastTime = time.Now()
393 user.Sort = time.Now().UnixNano() / 1e6
394 user.Role = USER_ROLE_ADMINISTRATOR
395 user.Username = adminUsername
396 user.Password = util.GetBcrypt(adminPassword)
397 user.SpaceUuid = space.Uuid
398 user.Status = USER_STATUS_OK
399
400 db4 := db.Create(user)
401 this.PanicError(db4.Error)
402
403 space.UserUuid = user.Uuid
404 db5 := db.Save(space)
405 this.PanicError(db5.Error)
406
407 return this.Success("OK")

Callers

nothing calls this directly

Calls 12

openDbConnectionMethod · 0.95
closeDbConnectionMethod · 0.95
BadRequestI18nFunction · 0.92
BadRequestFunction · 0.92
NewV4Function · 0.92
GetBcryptFunction · 0.92
StringMethod · 0.80
SuccessMethod · 0.80
PanicErrorMethod · 0.65
CreateMethod · 0.65
CountMethod · 0.45
SaveMethod · 0.45

Tested by

no test coverage detected