Ejemplo n.º 1
0
        private async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> argument)
        {
            var message     = await argument;
            var userprofile = context.GetProfile();

            if (userprofile == null || string.IsNullOrWhiteSpace(userprofile.AccessToken))
            {
                if (message.Text.StartsWith("token:"))
                {
                    var token      = message.Text.Remove(0, "token:".Length);
                    var validtoken = await SitecoreAuthenticationAPI.Instance().ValidateAccessToken(token);

                    if (validtoken)
                    {
                        var profile = await SitecoreUserManagementAPI.Instance(token).GetProfile();

                        userprofile = new Models.UserProfile
                        {
                            AccessToken         = token,
                            FullName            = profile.FullName,
                            Login               = profile.UserName,
                            IsAdministrator     = profile.IsAdministrator,
                            Roles               = profile.Roles,
                            ApplicationInsights = profile.ApplicationInsights
                        };

                        context.SetProfile(userprofile);
                        context.Done(context.GetUnauthorizedMessageText());
                        return;
                    }
                }

                await context.PostAsync("Hi I'm Sitecore Bot! I don't really know who you are, so you have to show me some proof!");
                await LogIn(context);

                context.Wait(MessageReceived);
                return;
            }

            var authenticated = await userprofile.IsAuthenticated();

            if (!authenticated)
            {
                userprofile.AccessToken = null;
                context.SetProfile(userprofile);

                await context.PostAsync("Okay, I don't really remember who you were, could you please identify yourself?");
                await LogIn(context);

                context.Wait(MessageReceived);
                return;
            }

            return;
        }
        public static IForm <CreateUser> BuildForm()
        {
            var form = new FormBuilder <CreateUser>()
                       .Message("Let's get some more details for the user!")
                       .Field(nameof(FullName))
                       .Field(nameof(UserName), validate: async(state, value) =>
            {
                var exists = await SitecoreUserManagementAPI.Instance().UserExists((string)value);
                return(new ValidateResult {
                    IsValid = !exists, Value = value, Feedback = exists ? $"Username {value} is already taken." : $"Lucky you! Username {value} is available!"
                });
            })
                       .Field(nameof(EmailAddress))
                       .Confirm("Is the emailaddress {EmailAddress} correct?")
                       //.Field(nameof(IsAdministrator), prompt: "Do you want this user to be an administrator?")
                       .AddRemainingFields()
                       .Confirm("Do you want to create a new user for {FullName} ({UserName})?");

            form.Configuration.DefaultPrompt.ChoiceStyle = ChoiceStyleOptions.Buttons;

            return(form.Build());
        }
Ejemplo n.º 3
0
        public async Task ListUser(IDialogContext context, LuisResult result)
        {
            var isAuthenticated = await context.IsAuthenticated();

            if (!isAuthenticated)
            {
                context.SetUnauthorizedMessageText(result.Query);
                await context.Forward(new AuthenticationDialog(), this.ResumeAfterAuth, context.MakeMessage(), CancellationToken.None);
            }
            else if (context.IsAdministrator())
            {
                EntityRecommendation domain;
                EntityRecommendation role;
                result.TryFindEntity(Entities.Domain, out domain);
                result.TryFindEntity(Entities.Role, out role);

                await context.PostAsync(context.CreateTypingActivity());

                var users = await SitecoreUserManagementAPI.Instance(context.AccessToken()).GetUsers(domain == null ? null : domain.Entity, role == null ? null : role.Entity);

                StringBuilder sb = new StringBuilder();

                if (users.Count == 0)
                {
                    if (domain != null && role != null)
                    {
                        sb.AppendLine($"I'm sorry, we don't have {role.Entity} users in the {domain.Entity} domain  \n  \n");
                    }
                    else if (domain != null && role == null)
                    {
                        sb.AppendLine($"I'm sorry, we don't have {domain.Entity} domain  \n  \n");
                    }
                    else
                    {
                        sb.AppendLine($"I'm sorry, I haven't found any Sitecore users  \n  \n");
                    }
                }
                else
                {
                    if (domain != null && role != null)
                    {
                        sb.AppendLine($"Here is the list of {role.Entity} users in the {domain.Entity} domain  \n  \n");
                    }
                    else if (domain != null && role == null)
                    {
                        sb.AppendLine($"Here is the list of users in the {domain.Entity} domain  \n  \n");
                    }
                    else
                    {
                        sb.AppendLine($"Here is the list of Sitecore users  \n  \n");
                    }

                    users.ForEach(user =>
                    {
                        if (user.IsOnline)
                        {
                            sb.AppendLine($"{user.FullName} ({user.UserName}) is currently online!  \n");
                        }
                        else
                        {
                            sb.AppendLine(
                                user.LastActivity.HasValue
                                ? $"{user.FullName} ({user.UserName}) last activity {user.LastActivity.Value}  \n"
                                : $"{user.FullName} ({user.UserName})  \n");
                        }
                    });
                }

                await context.PostAsync(sb.ToString());
            }
        }
Ejemplo n.º 4
0
 public SitecoreUserManagementAPI Sitecore(string access_token)
 {
     return(_sitecore ?? (_sitecore = SitecoreUserManagementAPI.Instance(access_token)));
 }