Beispiel #1
0
        /// <summary> Determine if the specified project is an active source project. </summary>
        public bool IsSourceProject(string projectId)
        {
            IQueryable <SFProject> projectQuery = RealtimeService.QuerySnapshots <SFProject>();

            return(projectQuery.Any(p =>
                                    p.TranslateConfig.Source != null &&
                                    p.TranslateConfig.Source.ProjectRef == projectId &&
                                    p.TranslateConfig.TranslationSuggestionsEnabled
                                    ));
        }
Beispiel #2
0
        public async Task <bool> InviteAsync(string curUserId, string projectId, string email)
        {
            SFProject project = await GetProjectAsync(projectId);

            if (await RealtimeService.QuerySnapshots <User>()
                .AnyAsync(u => project.UserRoles.Keys.Contains(u.Id) && u.Email == email))
            {
                return(false);
            }
            SiteOptions siteOptions = SiteOptions.Value;

            if (!project.CheckingConfig.ShareEnabled && !IsProjectAdmin(project, curUserId))
            {
                throw new ForbiddenException();
            }

            // Invite a specific person. Reuse prior code, if any.
            SFProjectSecret projectSecret = await ProjectSecrets.UpdateAsync(
                p => p.Id == projectId && !p.ShareKeys.Any(sk => sk.Email == email),
                update => update.Add(p => p.ShareKeys,
                                     new ShareKey {
                Email = email, Key = _securityService.GenerateKey()
            }));

            if (projectSecret == null)
            {
                projectSecret = await ProjectSecrets.GetAsync(projectId);
            }
            string key = projectSecret.ShareKeys.Single(sk => sk.Email == email).Key;
            string url = $"{siteOptions.Origin}projects/{projectId}?sharing=true&shareKey={key}";
            string emailSpecificLinkMessage = _localizer[SharedResource.Keys.InviteLinkSharingOff];

            User inviter = await RealtimeService.GetSnapshotAsync <User>(curUserId);

            string subject      = _localizer[SharedResource.Keys.InviteSubject, project.Name, siteOptions.Name];
            var    greeting     = $"<p>{_localizer[SharedResource.Keys.InviteGreeting, "<p>", inviter.Name, project.Name, siteOptions.Name, $"<a href=\"{url}\">{url}</a><p>"]}";
            var    instructions = $"<p>{_localizer[SharedResource.Keys.InviteInstructions, siteOptions.Name, "<b>", "</b>"]}";
            var    pt           = $"<ul><li>{_localizer[SharedResource.Keys.InvitePTOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    google       = $"<li>{_localizer[SharedResource.Keys.InviteGoogleOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    facebook     = $"<li>{_localizer[SharedResource.Keys.InviteFacebookOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    withemail    = $"<li>{_localizer[SharedResource.Keys.InviteEmailOption, siteOptions.Name]}</li></ul></p><p></p>";
            var    signoff      = $"<p>{_localizer[SharedResource.Keys.InviteSignature, "<p>", siteOptions.Name]}</p>";
            var    emailBody    = $"{greeting}{emailSpecificLinkMessage}{instructions}{pt}{google}{facebook}{withemail}{signoff}";
            await _emailService.SendEmailAsync(email, subject, emailBody);

            return(true);
        }
Beispiel #3
0
        public async Task <bool> InviteAsync(string curUserId, string projectId, string email, string locale,
                                             string role)
        {
            SFProject project = await GetProjectAsync(projectId);

            if (await RealtimeService.QuerySnapshots <User>()
                .AnyAsync(u => project.UserRoles.Keys.Contains(u.Id) && u.Email == email))
            {
                return(false);
            }
            SiteOptions siteOptions = SiteOptions.Value;

            if (!project.CheckingConfig.ShareEnabled && !IsProjectAdmin(project, curUserId))
            {
                throw new ForbiddenException();
            }
            CultureInfo.CurrentUICulture = new CultureInfo(locale);
            // Remove the user sharekey if expired
            await ProjectSecrets.UpdateAsync(
                p => p.Id == projectId,
                update => update.RemoveAll(p => p.ShareKeys,
                                           sk => sk.Email == email && sk.ExpirationTime < DateTime.UtcNow)
                );

            DateTime expTime = DateTime.UtcNow.AddDays(14);

            // Invite a specific person. Reuse prior code, if any.
            SFProjectSecret projectSecret = await ProjectSecrets.UpdateAsync(
                p => p.Id == projectId && !p.ShareKeys.Any(sk => sk.Email == email),
                update => update.Add(p => p.ShareKeys,
                                     new ShareKey
            {
                Email          = email,
                Key            = _securityService.GenerateKey(),
                ExpirationTime = expTime,
                ProjectRole    = role
            }
                                     )
                );

            if (projectSecret == null)
            {
                projectSecret = await ProjectSecrets.GetAsync(projectId);

                int index = projectSecret.ShareKeys.FindIndex(sk => sk.Email == email);

                // Renew the expiration time of the valid key
                await ProjectSecrets.UpdateAsync(
                    p => p.Id == projectId && p.ShareKeys.Any(sk => sk.Email == email),
                    update => update.Set(p => p.ShareKeys[index].ExpirationTime, expTime)
                    );
            }
            string key         = projectSecret.ShareKeys.Single(sk => sk.Email == email).Key;
            string url         = $"{siteOptions.Origin}projects/{projectId}?sharing=true&shareKey={key}&locale={locale}";
            string linkExpires = _localizer[SharedResource.Keys.InviteLinkExpires];

            User inviter = await RealtimeService.GetSnapshotAsync <User>(curUserId);

            string subject      = _localizer[SharedResource.Keys.InviteSubject, project.Name, siteOptions.Name];
            var    greeting     = $"<p>{_localizer[SharedResource.Keys.InviteGreeting, "<p>", inviter.Name, project.Name, siteOptions.Name, $"<a href=\"{url}\">{url}</a><p>"]}";
            var    instructions = $"<p>{_localizer[SharedResource.Keys.InviteInstructions, siteOptions.Name, "<b>", "</b>"]}";
            var    pt           = $"<ul><li>{_localizer[SharedResource.Keys.InvitePTOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    google       = $"<li>{_localizer[SharedResource.Keys.InviteGoogleOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    facebook     = $"<li>{_localizer[SharedResource.Keys.InviteFacebookOption, "<b>", "</b>", siteOptions.Name]}</li>";
            var    withemail    = $"<li>{_localizer[SharedResource.Keys.InviteEmailOption, siteOptions.Name]}</li></ul></p><p></p>";
            var    signoff      = $"<p>{_localizer[SharedResource.Keys.InviteSignature, "<p>", siteOptions.Name]}</p>";
            var    emailBody    = $"{greeting}{linkExpires}{instructions}{pt}{google}{facebook}{withemail}{signoff}";
            await _emailService.SendEmailAsync(email, subject, emailBody);

            return(true);
        }