admin: add a button to edit the raw flag value instead of the checksum

This commit is contained in:
nemunaire 2019-01-21 13:35:09 +01:00
parent b205409679
commit eee2cd6a2f
4 changed files with 46 additions and 18 deletions

View File

@ -332,7 +332,15 @@ func updateExerciceFlag(flag fic.FlagKey, exercice fic.Exercice, body []byte) (i
flag.Help = uk.Help
flag.IgnoreCase = uk.IgnoreCase
flag.Checksum = uk.Value
if len(uk.Flag) > 0 {
var err error
flag.Checksum, err = flag.ComputeChecksum([]byte(uk.Flag))
if err != nil {
return nil, err
}
} else {
flag.Checksum = uk.Value
}
flag.ChoicesCost = uk.ChoicesCost
if uk.ValidatorRe != nil && len(*uk.ValidatorRe) > 0 {

View File

@ -1341,6 +1341,10 @@ angular.module("FICApp")
.controller("ExerciceFlagsController", function($scope, ExerciceFlag, $routeParams, $rootScope, $http) {
$scope.flags = ExerciceFlag.query({ exerciceId: $routeParams.exerciceId });
$scope.changeValue = function(flag) {
flag.value = undefined;
flag.show_raw = true;
}
$scope.addFlag = function() {
$scope.flags.push(new ExerciceFlag());
}

View File

@ -126,8 +126,13 @@
</div>
</div>
<div class="row">
<input type="text" id="kvalue{{flag.id}}" ng-model="flag.value" class="col form-control form-control-sm" placeholder="Condensat" ng-if="flag.id" title="Condensat">
<input type="text" id="kflag{{flag.id}}" ng-model="flag.flag" class="col form-control form-control-sm" placeholder="Chaîne brute à valider" ng-if="!flag.id" title="Chaîne brute à valider">
<div class="col input-group" ng-if="flag.id && !flag.show_raw" style="padding:0">
<input type="text" id="kvalue{{flag.id}}" ng-model="flag.value" class="form-control form-control-sm" placeholder="Condensat" title="Condensat">
<div class="input-group-append">
<button class="btn btn-sm btn-outline-secondary" ng-click="changeValue(flag)" title="Cliquez pour éditer la valeur brute du flag, au lieu du checksum" type="button"><span class="glyphicon glyphicon-erase" aria-hidden="true"></span></button>
</div>
</div>
<input type="text" id="kflag{{flag.id}}" ng-model="flag.flag" class="col form-control form-control-sm" placeholder="Chaîne brute à valider" ng-if="!flag.id || flag.show_raw" title="Chaîne brute à valider">
<div class="col-auto custom-control custom-checkbox ml-1">
<input type="checkbox" class="custom-control-input" id="kicase{{flag.id}}" ng-model="flag.ignorecase">
<label class="custom-control-label" for="kicase{{flag.id}}">Ignore case</label>
@ -138,8 +143,8 @@
</div>
</form>
<form ng-submit="testFlag(flag)">
<div class="input-group">
<input type="text" id="ktest{{flag.id}}" ng-model="flag.test_str" class="col form-control form-control-sm" placeholder="Test the flag">
<div class="input-group row">
<input type="text" id="ktest{{flag.id}}" ng-model="flag.test_str" class="form-control form-control-sm" placeholder="Test the flag">
<div class="input-group-append">
<button class="btn btn-sm btn-warning" type="submit"><span class="glyphicon glyphicon-play" aria-hidden="true"></span></button>
</div>

View File

@ -119,6 +119,28 @@ func (e Exercice) AddFlagKey(name string, help string, ignorecase bool, validato
}
}
// SetChecksumFromValue .
func (k FlagKey) ComputeChecksum(val []byte) (cksum []byte, err error) {
if k.IgnoreCase {
val = bytes.ToLower(val)
}
// Check that raw value passes through the regexp
if k.ValidatorRegexp != nil {
if val, err = ExecValidatorRegexp(*k.ValidatorRegexp, val); err != nil {
return
}
}
// Check that the value is not empty
if len(val) == 0 {
err = errors.New("Empty flag after applying filters")
}
hash := getHashedFlag(val)
return hash[:], err
}
// Update applies modifications back to the database.
func (k FlagKey) Update() (int64, error) {
if k.ValidatorRegexp != nil {
@ -220,21 +242,10 @@ func (k FlagKey) Check(v interface{}) int {
val = va
}
if k.IgnoreCase {
val = bytes.ToLower(val)
}
// Check that raw value passes through the regexp
if k.ValidatorRegexp != nil {
val, _ = ExecValidatorRegexp(*k.ValidatorRegexp, val)
}
// Check that the value is not empty
if len(val) == 0 {
hash, err := k.ComputeChecksum(val)
if err != nil {
return 1
}
hash := getHashedFlag(val)
if len(k.Checksum) != len(hash) {
return 1
}