frontend: team registration

This commit is contained in:
nemunaire 2017-12-21 22:18:18 +01:00
parent bc135d00c5
commit 184714aeeb
10 changed files with 208 additions and 28 deletions

View File

@ -138,7 +138,7 @@ func treat(raw_path string) {
if len(spath) == 3 {
if spath[1] == "_registration" {
treatRegistration(raw_path)
treatRegistration(raw_path, spath[2])
} else if team, err := fic.GetTeamByInitialName(spath[1]); err != nil {
log.Println("[ERR]", err)
} else if spath[2] == "name" {

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
@ -11,23 +12,48 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func treatRegistration(pathname string) {
var keys map[string]string
type uTeamRegistration struct {
TeamName string
Members []fic.Member
}
func treatRegistration(pathname string, initial_name string) {
var nTeam uTeamRegistration
if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
log.Println("[ERR]", err)
} else if err := json.Unmarshal(cnt_raw, &keys); err != nil {
} else if err := json.Unmarshal(cnt_raw, &nTeam); err != nil {
log.Println("[ERR]", err)
} else if validTeamName(keys["name"]) {
if team, err := fic.CreateTeam(keys["name"], rand.Uint32()); err != nil {
log.Printf("[ERR] Unable to register new team %s: %s\n", keys["name"], err)
} else if validTeamName(nTeam.TeamName) {
if team, err := fic.RegisterTeam(initial_name, nTeam.TeamName, uint32(rand.Int31n(16581376))); err != nil {
log.Printf("[ERR] Unable to register new team %s: %s\n", nTeam.TeamName, err)
} else {
for _, m := range nTeam.Members {
// Force Id to 0, as it shouldn't have been defined yet
m.Id = 0
if err := team.GainMember(m); err != nil {
log.Println("[WRN] Unable to add member (", m, ") to team (", team, "): ", err)
}
}
if err := os.Remove(pathname); err != nil {
log.Println("[WRN]", err)
}
if _, err := fic.NewEvent(fmt.Sprintf("Souhaitons bonne chance à l'équipe <strong>%s</strong> qui vient de nous rejoindre !", team.Name), "alert-info"); err != nil {
log.Println("[WRN] Unable to create event:", err)
}
os.MkdirAll(path.Join(SubmissionDir, team.InitialName), 0777)
os.MkdirAll(path.Join(TeamsDir, team.InitialName), 0777)
}
if err := os.Remove(pathname); err != nil {
log.Println("[ERR]", err)
go func() {
if err := genTeamMyFile(team); err != nil {
log.Println("Team generation error: ", err)
}
if err := genTeamsFile(); err != nil {
log.Println("teams.json generation error: ", err)
}
}()
}
}
}

View File

@ -41,7 +41,7 @@ func main() {
// Register handlers
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), submissionTeamChecker{"name change", ChNameHandler, *teamsDir}))
http.Handle(fmt.Sprintf("%s/openhint/", *prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", *prefix), submissionTeamChecker{"opening hint", HintHandler, *teamsDir}))
http.Handle(fmt.Sprintf("%s/registration", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration", *prefix), submissionChecker{"registration", RegistrationHandler}))
http.Handle(fmt.Sprintf("%s/registration/", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration/", *prefix), submissionChecker{"registration", RegistrationHandler}))
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), submissionTeamChecker{"submission", SubmissionHandler, *teamsDir}))
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, fronttime.TimeHandler{}))

View File

@ -1,7 +1,6 @@
package main
import (
"io/ioutil"
"log"
"net/http"
"path"
@ -16,28 +15,26 @@ func RegistrationHandler(w http.ResponseWriter, r *http.Request, sURL []string)
return
}
if len(sURL) < 1 || len(sURL[0]) == 0 {
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
return
}
teamInitialName := sURL[0]
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
} else if r.ContentLength < 0 || r.ContentLength > 4095 {
http.Error(w, "{\"errmsg\":\"Requête trop longue ou de taille inconnue\"}", http.StatusRequestEntityTooLarge)
return
}
if tmpfile, err := ioutil.TempFile(path.Join(SubmissionDir, "_registration"), ""); err != nil {
log.Println("Unable to generate registration file:", err)
if err := saveFile(path.Join(SubmissionDir, "_registration", teamInitialName), r); err != nil {
log.Println("Unable to open registration file:", err)
http.Error(w, "{\"errmsg\":\"Internal server error. Please retry in few seconds.\"}", http.StatusInternalServerError)
} else {
// The file will be reopened by saveFile
tmpfile.Close()
if err := saveFile(tmpfile.Name(), r); err != nil {
log.Println("Unable to open registration file:", err)
http.Error(w, "{\"errmsg\":\"Internal server error. Please retry in few seconds.\"}", http.StatusInternalServerError)
} else {
// File enqueued for backend treatment
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
}
// File enqueued for backend treatment
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
}
}

View File

@ -83,6 +83,11 @@
<span class="teamname">{{ my.name }}</span>
</a>
</span>
<span class="navbar-text text-light" ng-show="!my.team_id && time.start" ng-cloak>
<a ng-href="/register" class="badge badge-warning" role="button">
Inscription
</a>
</span>
</nav>
<div class="container">

View File

@ -13,6 +13,10 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
controller: "RankController",
templateUrl: "views/rank.html"
})
.when("/register", {
controller: "RegisterController",
templateUrl: "views/register.html"
})
.when("/videos", {
controller: "VideosController",
templateUrl: "views/videos.html"
@ -74,7 +78,7 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
updTime();
$interval(updTime, 1000);
})
.controller("DataController", function($sce, $scope, $http, $rootScope, $timeout) {
.controller("DataController", function($sce, $scope, $http, $rootScope, $timeout, $location) {
var actMenu = function() {
if ($scope.my && $scope.themes) {
angular.forEach($scope.themes, function(theme, key) {
@ -146,6 +150,10 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
});
});
}
}, function(response) {
if (!$scope.my && response.status == 404) {
$location.url("/register");
}
});
console.log("refresh!");
}
@ -358,6 +366,76 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
});
};
})
.controller("RegisterController", function($scope, $rootScope, $location, $http) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.title = "Bienvenue au challenge forensic !";
$rootScope.authors = null;
$scope.members = [{}];
$scope.AddMember = function() {
$scope.members.push({});
}
$scope.RemoveMember = function(k) {
$scope.members.splice(k, 1);
}
$scope.Validate = function() {
if ($scope.teamName.length <= 3) {
$('#teamName').addClass("is-invalid")
return;
} else {
$('#vldBtn').removeClass("input-group-btn");
$('#vldBtn').css("display", "none");
$scope.part2 = true;
}
}
$scope.rsubmit = function() {
if (!$scope.part2)
return $scope.Validate();
// Remove empty members
$scope.members = $scope.members.filter(function(m) {
return ((m.lastname != undefined && m.lastname != "") || (m.firstname != undefined && m.firstname != "") || (m.nickname != undefined && m.nickname != ""));
});
if ($scope.members.length == 0) {
$scope.messageClass = {"text-danger": true};
$scope.message = "Veuillez ajouter au moins un membre dans votre équipe !";
$scope.members.push({});
return;
}
$http({
url: "/registration",
method: "POST",
data: {
teamName: $scope.teamName,
members: $scope.members,
}
}).then(function(response, status, header, config) {
$scope.messageClass = {"text-success": true};
$scope.message = response.data.errmsg;
$rootScope.refresh();
if ($scope.my)
$location.url("/");
}, function(response) {
$scope.messageClass = {"text-danger": true};
console.log(response);
if (response.data && response.data.errmsg)
$scope.message = response.data.errmsg;
else
$scope.message = "Une erreur est survenue lors de l'inscription de l'équipe. Veuillez réessayer dans quelques instants.";
});
}
if ($scope.my) {
$location.url("/");
}
})
.controller("RankController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;

View File

@ -0,0 +1,62 @@
<div class="jumbotron" style="text-indent: 1em">
<p>
Félicitations&nbsp;! vous êtes maintenant authentifié auprès de notre
serveur&nbsp;!
</p>
<p>
Votre équipe n'est pas encore enregistrée sur notre serveur. Afin de
pouvoir participer au challenge, nous vous remercions de bien vouloir
remplir le formulaire d'inscription suivant&nbsp;:
</p>
<form ng-submit="rsubmit()">
<div class="row">
<label for="teamName" class="col col-form-label">Nom d'équipe</label>
<div class="col-sm-10">
<div class="input-group">
<input type="text" class="form-control" id="teamName" ng-model="teamName" placeholder="" autofocus required>
<div class="invalid-feedback">
Veuillez indiquer un nom d'équipe valide.
</div>
<span class="input-group-btn" id="vldBtn">
<button class="btn btn-info" type="button" ng-click="Validate()">Valider</button>
</span>
</div>
</div>
</div>
<div ng-if="part2">
<h4 style="text-indent: 0; margin-top: 20px">
Membres d'équipe
<button class="btn btn-sm btn-success" type="button" ng-click="AddMember()">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter un membre
</button>
</h4>
<p ng-if="message" ng-class="messageClass" ng-bind="message"></p>
<div class="row form-group" ng-repeat="(mid, member) in members">
<div class="col-sm">
<input type="text" class="form-control" ng-model="member.lastname" placeholder="Nom" autofocus>
</div>
<div class="col-sm">
<input type="text" class="form-control" ng-model="member.firstname" placeholder="Prénom">
</div>
<div class="col-sm">
<input type="text" class="form-control" ng-model="member.nickname" placeholder="Pseudo">
</div>
<div class="col-sm">
<input type="text" class="form-control" ng-model="member.company" placeholder="Entreprise">
</div>
<div class="col-sm-auto">
<button class="btn btn-danger" type="button" ng-click="RemoveMember(mid)">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
</div>
</div>
<button class="btn btn-info" style="margin-left: 40%;" type="submit">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
S'inscrire
</button>
</div>
</form>
</div>

View File

@ -68,7 +68,7 @@ CREATE TABLE IF NOT EXISTS themes(
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS teams(
id_team INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
initial_name VARCHAR(255) NOT NULL,
initial_name VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
color INTEGER NOT NULL
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;

View File

@ -52,7 +52,15 @@ func (t Team) AddMember(firstname string, lastname string, nickname string, comp
}
func (t Team) GainMember(m Member) error {
if res, err := DBExec("UPDATE team_members SET id_team = ? WHERE id_member = ?", t.Id, m.Id); err != nil {
if m.Id == 0 {
if res, err := DBExec("INSERT INTO team_members (id_team, firstname, lastname, nickname, company) VALUES (?, ?, ?, ?, ?)", t.Id, m.Firstname, m.Lastname, m.Nickname, m.Company); err != nil {
return err
} else if _, err := res.LastInsertId(); err != nil {
return err
} else {
return nil
}
} else if res, err := DBExec("UPDATE team_members SET id_team = ? WHERE id_member = ?", t.Id, m.Id); err != nil {
return err
} else if _, err := res.RowsAffected(); err != nil {
return err

View File

@ -62,6 +62,10 @@ func GetTeamByInitialName(initialName string) (Team, error) {
func CreateTeam(name string, color uint32) (Team, error) {
re := regexp.MustCompile("[^a-zA-Z0-9]+")
initialName := re.ReplaceAllLiteralString(name, "_")
return RegisterTeam(initialName, name, color)
}
func RegisterTeam(initialName string, name string, color uint32) (Team, error) {
if res, err := DBExec("INSERT INTO teams (initial_name, name, color) VALUES (?, ?, ?)", initialName, name, color); err != nil {
return Team{}, err
} else if tid, err := res.LastInsertId(); err != nil {