/// <summary> /// Checks whether a given user is whitelisted. /// </summary> /// <param name="user">The Slack user to check.</param> /// <returns>Whether the given user is whitelisted.</returns> public async Task <bool> IsUserWhitelisted(SlackUser user) { user.Guard(); await InitializeWhitelistIfNeeded(); return(whitelistedUserIds.Contains(user.Id)); }
ProfileValidationResult(bool isValid, SlackUser user, string errors, Uri imageURL) { user.Guard(); IsValid = isValid; User = user; Errors = errors; ImageURL = imageURL; }
/// <summary> /// Whitelists the given user. /// </summary> /// <param name="user">The Slack user to whitelist.</param> /// <returns>No object or value is returned by this method when it completes.</returns> public async Task WhitelistUser(SlackUser user) { user.Guard(); if (await IsUserWhitelisted(user)) { return; } var blobRef = container.GetBlockBlobReference(user.Id); await blobRef.UploadTextAsync(user.Id); whitelistedUserIds.Add(user.Id); }
/// <summary> /// Validates a Slack user's profile image. /// The image is valid if it's recognized as an /// image of a single human. /// </summary> /// <param name="user">The Slack user to validate.</param> /// <returns>The result of the face detection.</returns> public async Task <FaceDetectionResult> ValidateProfileImage(SlackUser user) { user.Guard(); if (string.IsNullOrEmpty(user.Image)) { throw new ArgumentException(nameof(user)); } try { if (await faceWhitelist.IsUserWhitelisted(user)) { return(FaceDetectionResult.Valid); } const int Tries = 9; for (var i = 0; i < Tries; ++i) { try { var faces = await faceServiceClient.DetectAsync(user.Image, false); switch (faces.Length) { case 0: return(new FaceDetectionResult("Kunne ikke se et ansikt i bildet ditt. Last opp et profilbilde av deg selv.")); case 1: return(FaceDetectionResult.Valid); default: return(new FaceDetectionResult("Fant flere ansikter i bildet litt. Last opp et profilbilde av deg selv.")); } } catch (FaceAPIException) { await Task.Delay(delay *i); } } logger.Error("Did not complete image validation for " + user.Name); return(FaceDetectionResult.Valid); } catch (Exception e) { logger.Error(e, "Face detection failed"); return(FaceDetectionResult.Valid); } }
/// <summary> /// Validates that a user profile is complete. /// </summary> /// <param name="user">The user to be validated.</param> /// <returns>The result of the validation.</returns> public async Task <ProfileValidationResult> ValidateProfile(SlackUser user) { user.Guard(); if (string.IsNullOrEmpty(user.Name)) { throw new ArgumentException("Name cannot be empty.", nameof(user)); } var errors = new StringBuilder(); ValidateEmail(user, errors); if (string.IsNullOrEmpty(user.FirstName)) { errors.AppendLine("Fornavn må registreres slik at folk vet hvem du er."); } if (string.IsNullOrEmpty(user.LastName)) { errors.AppendLine("Etternavn må registreres slik at folk vet hvem du er."); } if (string.IsNullOrEmpty(user.WhatIDo)) { errors.AppendLine("Feltet \"What I do\" må inneholde team og hva du kan i DIPS."); } var imageWasSuspect = await ValidateProfileImage(user, errors); var actualErrors = errors.ToString(); return(actualErrors.Length == 0 || user.IsBot || user.Deleted || user.Name == "slackbot" ? ProfileValidationResult.Valid(user) : new ProfileValidationResult(user, $"Hei <@{user.Id}>, jeg har sett gjennom profilen din og den har følgende mangler:" + $"{Environment.NewLine}{Environment.NewLine}{actualErrors}{Environment.NewLine}" + "Se https://github.com/DIPSAS/community/blob/master/communication/slack-guidelines.md for hva som kreves av en fullt utfylt profil." + $"{Environment.NewLine}Ta kontakt med #admins dersom du har spørsmål.", imageWasSuspect ? new Uri(user.Image) : null)); }
/// <summary> /// Creates the SlackProfileValidator. /// </summary> /// <param name="adminUser">The admin of this Slack team.</param> /// <param name="faceDetectionClient"></param> public SlackProfileValidator(SlackUser adminUser, IFaceDetectionClient faceDetectionClient) { adminUser.Guard(); adminUserId = adminUser.Id; this.faceDetectionClient = faceDetectionClient ?? throw new ArgumentNullException(nameof(faceDetectionClient)); }