diff --git a/admin/api/certificate.go b/admin/api/certificate.go index e0ee3cb5..4eeb91ac 100644 --- a/admin/api/certificate.go +++ b/admin/api/certificate.go @@ -52,6 +52,25 @@ func init() { } }))) + router.GET("/api/teams/:tid/associations", apiHandler(teamHandler( + func(team fic.Team, _ []byte) (interface{}, error) { + return pki.GetTeamAssociations(TeamsDir, team.Id) + }))) + router.POST("/api/teams/:tid/associations/:assoc", apiHandler(teamAssocHandler( + func(team fic.Team, assoc string, _ []byte) (interface{}, error) { + if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, assoc)); err != nil { + return nil, err + } + return "\"" + assoc + "\"", nil + }))) + router.DELETE("/api/teams/:tid/associations/:assoc", apiHandler(teamAssocHandler( + func(team fic.Team, assoc string, _ []byte) (interface{}, error) { + if err := os.Remove(path.Join(TeamsDir, assoc)); err != nil { + return nil, err + } + return "null", nil + }))) + router.GET("/api/certs/", apiHandler(getCertificates)) router.POST("/api/certs/", apiHandler(generateClientCert)) router.DELETE("/api/certs/", apiHandler(func(_ httprouter.Params, _ []byte) (interface{}, error) { return fic.ClearCertificates() })) diff --git a/admin/api/handlers.go b/admin/api/handlers.go index b04a6865..3eee1d75 100644 --- a/admin/api/handlers.go +++ b/admin/api/handlers.go @@ -110,6 +110,19 @@ func teamHandler(f func(fic.Team, []byte) (interface{}, error)) func(httprouter. } } +func teamAssocHandler(f func(fic.Team, string, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) { + return func(ps httprouter.Params, body []byte) (interface{}, error) { + var team fic.Team + + teamHandler(func (tm fic.Team, _ []byte) (interface{}, error) { + team = tm + return nil, nil + })(ps, body) + + return f(team, string(ps.ByName("assoc")), body) + } +} + func themeHandler(f func(fic.Theme, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) { return func(ps httprouter.Params, body []byte) (interface{}, error) { if thid, err := strconv.ParseInt(string(ps.ByName("thid")), 10, 64); err != nil { diff --git a/admin/api/team.go b/admin/api/team.go index 8c32692d..28d4ee5e 100644 --- a/admin/api/team.go +++ b/admin/api/team.go @@ -180,7 +180,13 @@ func disableInactiveTeams(_ httprouter.Params, _ []byte) (interface{}, error) { return nil, err } - if len(serials) == 0 { + var assocs []string + assocs, err = pki.GetTeamAssociations(TeamsDir, team.Id) + if err != nil { + return nil, err + } + + if len(serials) == 0 && len(assocs) == 0 { if team.Active { team.Active = false team.Update() diff --git a/admin/pki/team.go b/admin/pki/team.go index ead52313..2263e32c 100644 --- a/admin/pki/team.go +++ b/admin/pki/team.go @@ -47,3 +47,21 @@ func GetTeamSerials(dirname string, id_team int64) (serials []uint64, err error) } return } + +func GetTeamAssociations(dirname string, id_team int64) (teamAssocs []string, err error) { + // As futher comparaisons will be made with strings, convert it only one time + str_tid := fmt.Sprintf("%d", id_team) + + var assocs []string + if assocs, err = GetAssociations(dirname); err != nil { + return + } else { + for _, assoc := range assocs { + var tid string + if tid, err = os.Readlink(path.Join(dirname, assoc)); err == nil && tid == str_tid && !strings.HasPrefix(assoc, SymlinkPrefix) { + teamAssocs = append(teamAssocs, assoc) + } + } + } + return +} diff --git a/admin/static/js/app.js b/admin/static/js/app.js index ebf32265..1d3c626b 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -179,6 +179,9 @@ angular.module("FICApp") .factory("TeamCertificate", function($resource) { return $resource("/api/teams/:teamId/certificates", { teamId: '@id' }) }) + .factory("TeamAssociation", function($resource) { + return $resource("/api/teams/:teamId/associations/:assoc", { teamId: '@teamId', assoc: '@assoc' }) + }) .factory("TeamMember", function($resource) { return $resource("/api/teams/:teamId/members", { teamId: '@id' }, { 'save': {method: 'PUT'}, @@ -1498,29 +1501,12 @@ angular.module("FICApp") } } }) - .controller("TeamController", function($scope, $rootScope, $location, Team, TeamMember, TeamCertificate, $routeParams, $http) { + .controller("TeamController", function($scope, $rootScope, $location, Team, TeamMember, $routeParams) { if ($scope.team && $scope.team.id) $routeParams.teamId = $scope.team.id; $scope.team = Team.get({ teamId: $routeParams.teamId }); $scope.fields = ["name", "color"]; - $scope.certificates = TeamCertificate.query({ teamId: $routeParams.teamId }); - - $scope.dissociateCertificate = function(certificate) { - $http({ - url: "/api/certs/" + certificate.id, - method: "PUT", - data: { - id_team: null - } - }).then(function(response) { - $scope.certificates = TeamCertificate.query({ teamId: $routeParams.teamId }); - $rootScope.newBox('success', 'Certificate successfully dissociated!'); - }, function(response) { - $rootScope.newBox('danger', 'An error occurs when dissociating certiticate:', response.data.errmsg); - }); - } - $scope.saveTeam = function() { if (this.team.id) { this.team.$update(); @@ -1544,6 +1530,50 @@ angular.module("FICApp") $location.url("/teams/" + $scope.team.id + "/score"); } }) + .controller("TeamCertificatesController", function($scope, $rootScope, TeamCertificate, $routeParams, $http) { + $scope.certificates = TeamCertificate.query({ teamId: $routeParams.teamId }); + + $scope.dissociateCertificate = function(certificate) { + $http({ + url: "/api/certs/" + certificate.id, + method: "PUT", + data: { + id_team: null + } + }).then(function(response) { + $scope.certificates = TeamCertificate.query({ teamId: $routeParams.teamId }); + $rootScope.newBox('success', 'Certificate successfully dissociated!'); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when dissociating certiticate:', response.data.errmsg); + }); + } + }) + .controller("TeamAssociationsController", function($scope, $rootScope, TeamAssociation, $routeParams, $http) { + $scope.associations = TeamAssociation.query({ teamId: $routeParams.teamId }); + $scope.form = { "newassoc": "" }; + + $scope.addAssociation = function() { + if ($scope.form.newassoc) { + TeamAssociation.save({ teamId: $scope.team.id, assoc: $scope.form.newassoc }).$promise.then( + function() { + $scope.form.newassoc = ""; + $scope.associations = TeamAssociation.query({ teamId: $routeParams.teamId }); + }, function(response) { + if (response.data) + $rootScope.newBox('danger', 'An error occurs when creating user association: ', response.data.errmsg); + }); + } + } + + $scope.dropAssociation = function(assoc) { + TeamAssociation.delete({ teamId: $routeParams.teamId, assoc: assoc }).$promise.then( + function() { + $scope.associations = TeamAssociation.query({ teamId: $routeParams.teamId }); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when removing user association: ', response.data.errmsg); + }); + } + }) .controller("TeamHistoryController", function($scope, TeamHistory, $routeParams, $http, $rootScope) { $scope.history = TeamHistory.query({ teamId: $routeParams.teamId }); $scope.delHistory = function(row) { diff --git a/admin/static/views/team-edit.html b/admin/static/views/team-edit.html index 0be2b267..e2ebec58 100644 --- a/admin/static/views/team-edit.html +++ b/admin/static/views/team-edit.html @@ -70,7 +70,7 @@ -
+
Certificates @@ -89,7 +89,7 @@
Date de création - Télécharger + Télécharger
{{ cert.creation }}
Mot de passe
@@ -100,6 +100,31 @@
+
+
+ + Utilisateurs associés +
+
+
    +
  • + {{ a }} + +
  • +
  • +
    + + + + +
    +
  • +
+
+
+