예제 #1
0
        public AccountUserApiModel(WikiDownUser user)
        {
            this.Email    = user.Email;
            this.UserName = user.UserName;

            this.IsRoot      = (user.UserName == ArticleAccessHelper.RootAccountName);
            this.AccessLevel = (int)ArticleAccessHelper.GetAccessLevel(user.Roles);
        }
예제 #2
0
        private async void SignInAsync(WikiDownUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut();

            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            var authenticationProperties = new AuthenticationProperties {
                IsPersistent = isPersistent
            };

            AuthenticationManager.SignIn(authenticationProperties, identity);
        }
예제 #3
0
        private IEnumerable <string> GetRoles(IPrincipal principal, WikiDownUser user)
        {
            var userRoles = ArticleAccessHelper.GetRoles(this.AccessLevel);

            if (user != null)
            {
                var userAccessLevel      = ArticleAccessHelper.GetAccessLevel(user.Roles);
                var principalAccessLevel = principal.GetAccessLevel();
                if (userAccessLevel > principalAccessLevel)
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
            }

            return(userRoles);
        }
예제 #4
0
        public async Task <WikiDownUser> Save(IPrincipal principal, UserManager <WikiDownUser> userManager)
        {
            var user = await userManager.FindByNameAsync(this.UserName);

            var roles = this.GetRoles(principal, user);

            if (user != null)
            {
                if (user.UserName == principal.Identity.Name)
                {
                    var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles);
                    if (userAccessLevel < ArticleAccessLevel.Admin)
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }
                }

                user.SetRoles(roles);
                user.SetEmail(this.Email);

                if (!string.IsNullOrWhiteSpace(this.Password))
                {
                    await userManager.RemovePasswordAsync(user.Id);

                    await userManager.AddPasswordAsync(user.Id, this.Password);
                }

                await userManager.UpdateAsync(user);
            }
            else
            {
                user = new WikiDownUser(this.UserName)
                {
                    Roles = roles
                };
                user.SetEmail(this.Email);

                await userManager.CreateAsync(user, this.Password);
            }

            return(user);
        }