Ejemplo n.º 1
0
        public ActionResult Edit(AdminUserEditModel model, string[] selectRoles)
        {
            if (ModelState.IsValid)
            {
                var user = userManager.FindById(model.AdminId);
                if (model.WillChangePassword)
                {
                    user.PasswordHash = userManager.PasswordHasher.HashPassword(model.Password);
                }
                user.Email       = model.Email;
                user.PhoneNumber = model.PhoneNumber;
                user.Roles.Clear();
                foreach (string roleName in selectRoles)
                {
                    var         Role     = roleManager.FindByName(roleName);
                    AppUserRole userRole = new AppUserRole {
                        RoleId = Role.Id,
                        UserId = user.Id
                    };
                    user.Roles.Add(userRole);
                }

                var updateResult = userManager.Update(user);
                if (updateResult.Succeeded)
                {
                    TempData["message"] = "정보 수정이 완료되었습니다.";
                    return(RedirectToAction("Index"));
                }
                else
                {
                    AddErrorsFromResult(updateResult);
                }
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        private void CreateRoles(AppDbContext context)
        {
            var roleManager = new AppRoleManager(context);

            if (roleManager.FindByName("Administrator") == null)
            {
                roleManager.Create(new IdentityRole("Administrator"));
            }

            if (roleManager.FindByName("Functional") == null)
            {
                roleManager.Create(new IdentityRole("Functional"));
            }
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> GetApplicationUser(string id)
        {
            var user = await AppUserManager.FindByIdAsync(id);

            var coachRole = AppRoleManager.FindByName("Coach");

            if (user == null)
            {
                return(NotFound());
            }
            var response = new GetUserResponse
            {
                Item = new UserHttpResponse
                {
                    Id        = user.Id,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    IsCoach   = user.Roles.Any(j => j.RoleId == coachRole.Id),
                    Email     = user.UserName,
                    RoleIds   = user.Roles.Select(i => i.RoleId).ToList(),
                    Timezone  = user.Timezone,
                    CVLink    = user.CVLink,
                    Webex     = user.Webex,
                },
            };

            response.UsageCount = AppDb.CoachingPrograms
                                  .Count(i =>
                                         i.Coach.Id == id ||
                                         i.Coachee.Id == id);
            return(Ok(response));
        }
Ejemplo n.º 4
0
        public static void InitializeIdentityForEF(AppDbContext context)
        {
            var currentContext = HttpContext.Current;

            var ctx = currentContext.GetOwinContext();


            AppUserManager userManager = ctx.Get <AppUserManager>();
            AppRoleManager roleManager = ctx.Get <AppRoleManager>();

            string roleName  = "Administrator";
            string firstName = "ufuk";
            string lastName  = "aytas";
            string email     = "*****@*****.**";
            string password  = "******";

            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new Role
                {
                    Name = roleName
                };

                roleManager.Create(role);
            }



            User user = userManager.FindByName(email);

            if (user == null)
            {
                user = new User
                {
                    UserName       = email,
                    Email          = email,
                    EmailConfirmed = true,
                    FirstName      = firstName,
                    LastName       = lastName
                };

                userManager.Create(user, password);

                userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);

            if (!rolesForUser.Contains(roleName))
            {
                userManager.AddToRole(user.Id, roleName);
            }
        }
Ejemplo n.º 5
0
        public IHttpActionResult GetByName(string roleName)
        {
            var role = AppRoleManager.FindByName(roleName);

            if (role != null)
            {
                return(Ok(TheModelFactory.Create(role)));
            }
            return(NotFound());
        }
Ejemplo n.º 6
0
        private void AddUsersToRoles(AppDbContext context)
        {
            var roleManager = new AppRoleManager(context);
            var userManager = new AppUserManager(context);

            var adminUser = userManager.FindByName("admin");
            var anonUser  = userManager.FindByName("anonymous");

            var adminRole = roleManager.FindByName("Administrator");
            var funcRole  = roleManager.FindByName("Functional");


            if (adminUser != null && adminRole != null)
            {
                userManager.AddToRole(adminUser.Id, adminRole.Name);
            }

            if (anonUser != null && funcRole != null)
            {
                userManager.AddToRole(anonUser.Id, funcRole.Name);
            }
        }
Ejemplo n.º 7
0
        public IEnumerable <UserHttpResponse> GetUsers()
        {
            var currentUser = AppUserManager.FindById(User.Identity.GetUserId());
            var coachRole   = AppRoleManager.FindByName("Coach");
            var users       = AppUserManager.Users
                              .Select(i => new UserHttpResponse
            {
                Id        = i.Id,
                FirstName = i.FirstName,
                LastName  = i.LastName,
                IsCoach   = i.Roles.Any(j => j.RoleId == coachRole.Id),
                Email     = i.UserName,
                Timezone  = i.Timezone,
                CVLink    = i.CVLink,
                Webex     = i.Webex,
            })
                              .ToList();

            return(users);
        }
Ejemplo n.º 8
0
        public IHttpActionResult GetUsersForAdmin(int pageNumber = 1, int pageSize = 25, String sort = "CreatedAt desc")
        {
            var currentUser = AppUserManager.FindById(User.Identity.GetUserId());
            var coachRole   = AppRoleManager.FindByName("Coach");
            var users       = (IQueryable <ApplicationUser>)AppUserManager.Users;

            if (String.IsNullOrEmpty(sort) || sort == "null")
            {
                users = users.OrderBy(i => i.UserName);
            }
            else
            {
                if (sort.EndsWith(","))
                {
                    sort = sort.TrimEnd(',');
                }
                users = users.OrderBy(sort);
            }
            var count = users.Count();

            users = users
                    .Skip((pageNumber - 1) * pageSize)
                    .Take(pageSize);
            return(Ok(new GetUserForAdminResponse
            {
                TotalItems = count,
                Items = users
                        .Select(i => new UserHttpResponse
                {
                    Id = i.Id,
                    FirstName = i.FirstName,
                    LastName = i.LastName,
                    IsCoach = i.Roles.Any(j => j.RoleId == coachRole.Id),
                    Email = i.UserName,
                    Timezone = i.Timezone,
                    CVLink = i.CVLink,
                    Webex = i.Webex,
                })
                        .ToList(),
            }));
        }
Ejemplo n.º 9
0
        public ActionResult AddUser(AdminUserCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData["message"] = "정보를 정확하게 입력해주세요.";
            }
            var user = new AppUser {
                UserName = model.UserName
            };

            if (model.SelectRoles != null)
            {
                foreach (var roleName in model.SelectRoles)
                {
                    var         Role     = roleManager.FindByName(roleName);
                    AppUserRole userRole = new AppUserRole {
                        RoleId = Role.Id,
                        UserId = user.Id
                    };
                    user.Roles.Add(userRole);
                }
                IdentityResult result = userManager.Create(user, model.Password);
                if (result.Succeeded)
                {
                    TempData["message"] = "관리자 계정이 생성되었습니다.";
                    return(RedirectToAction("Index"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            else
            {
                TempData["message"] = "역할을 선택해주세요.";
            }
            model.Roles = roleManager.Roles.ToList();
            return(View(model));
        }
        public static void Init(PaymentSystemContext context)
        {
            try
            {
                var userManager = new AppUserManager(new AppUserStore(context));
                var roleManager = new AppRoleManager(new AppRoleStore(context));

                if (!context.Roles.Any())
                {
                    /*
                     * foreach (var role in GetPreconfiguredRoles())
                     * {
                     *  context.Roles.Add(role);
                     * }
                     */

                    foreach (var role in GetPreconfiguredRoles())
                    {
                        if (roleManager.FindByName(role.Name) != null)
                        {
                            continue;
                        }

                        roleManager.Create(role);
                    }
                }

                if (!context.Users.Any())
                {
                    foreach (var user in GetPreconfiguredUsers())
                    {
                        if (userManager.FindByName(user.UserName) != null)
                        {
                            continue;
                        }

                        var result = userManager.Create(user, DEFAULT_PASSWD);
                        result = userManager.SetLockoutEnabled(user.Id, false);
                    }
                }

                if (!context.UserRoles.Any())
                {
                    /*
                     * foreach (var userRole in GetPreconfiguredUserRole())
                     * {
                     *  context.UserRoles.Add(userRole);
                     * }
                     */

                    // admin
                    userManager.AddToRole(ADMIN_ID, "Admin");
                    userManager.AddToRole(ADMIN_ID, "Membership");

                    // hangfire
                    userManager.AddToRole(HANGFIRE_ID, "Hangfire");

                    // berat
                    userManager.AddToRole(BERAT_ID, "Membership");

                    // sidus
                    userManager.AddToRole(SIDUS_ID, "Membership");

                    // zeynep
                    userManager.AddToRole(ZEYNEP_ID, "Membership");

                    // eymen
                    userManager.AddToRole(EYMEN_ID, "Membership");

                    // uras
                    userManager.AddToRole(URAS_ID, "Membership");
                }

                if (!context.AccrualLoan.Any())
                {
                    foreach (var accrualLoan in GetPreconfiguredAccrualLoan())
                    {
                        context.AccrualLoan.Add(accrualLoan);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 11
0
 public ApplicationRole FindByName(string roleName)
 {
     return(_roleManager.FindByName(roleName));
 }