Esempio n. 1
0
        public ActionResult AssignRole(int id)
        {
            var user     = userService.GetById(id);
            var userRole = new UserRole();

            userRole.UserId = id;
            userRole.RoleId = 1;
            userRoleService.Add(userRole);
            TempData["result"] = "Gán quyền thành công";
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public static void Initialize(IConfiguration configuration, IUserService userService, IRoleService roleService, IUserRoleService userRoleService)
        {
            string     adminId         = Guid.NewGuid().ToString();
            string     adminName       = configuration["AdminAccount:Name"];
            string     adminEmail      = configuration["AdminAccount:Email"];
            string     adminPassword   = configuration["AdminAccount:Password"];
            string     adminRoleId     = Guid.NewGuid().ToString();
            RoleFilter adminRoleFilter = new RoleFilter {
                Name = Role.Admin
            };

            try
            {
                roleService.Get(adminRoleFilter);
            }
            catch (ObjectNotFoundException)
            {
                RoleDto roleDto = new RoleDto
                {
                    Id   = adminRoleId,
                    Name = Role.Admin
                };

                roleService.Add(roleDto);
            }

            try
            {
                userService.Get(adminName, adminPassword);
            }
            catch (ObjectNotFoundException)
            {
                UserDto userDto = new UserDto
                {
                    Id       = adminId,
                    Name     = adminName,
                    Email    = adminEmail,
                    Password = adminPassword
                };

                userService.Add(userDto);

                UserRoleDto userRoleDto = new UserRoleDto
                {
                    RoleId = adminRoleId,
                    UserId = adminId
                };

                userRoleService.Add(userRoleDto);
            }
        }
Esempio n. 3
0
 public ActionResult Create(UserRoleViewModel userrole)
 {
     if (!ModelState.IsValid)
     {
         ErrorNotification("Marka Eklenemedi!");
         return(RedirectToAction("Create"));
     }
     _userRoleService.Add(new UserRole
     {
         UserId = userrole.UserId,
         RoleId = userrole.RoleId
     });
     SuccessNotification("Kayıt Eklendi.");
     return(RedirectToAction("Create"));
 }
 public ActionResult Create(UserRoleViewModel userRole)
 {
     if (!ModelState.IsValid)
     {
         ErrorNotification("Kayıt Eklenemedi!");
         return(RedirectToAction("Create"));
     }
     _userRoleService.Add(new UserRole
     {
         //TODO:Alanlar buraya yazılacak
         //Örn:BrandName = brand.BrandName,
     });
     SuccessNotification("Kayıt Eklendi.");
     return(RedirectToAction("UserRoleIndex"));
 }
Esempio n. 5
0
        public bool SetUserRoles(string userID, string roleIDs, out string msg)
        {
            SqlParameter param = new SqlParameter("@UserID", userID);

            userRoleService.ExecuteSql("DELETE FROM dbo.SysUserRole WHERE UserID=@UserID", out msg, param);
            roleIDs.Split(',').ToList().ForEach(roileid =>
            {
                var userRole = new SysUserRole()
                {
                    ID     = IdentityCreator.NextIdentity,
                    UserID = userID,
                    RoleID = roileid,
                };
                userRoleService.Add(userRole);
            });
            return(userRoleService.SaveChanges(out msg) > 0);
        }
Esempio n. 6
0
        public async Task <BaseResponse> CreateUserRole(List <UserRoleDTO> entity)
        {
            try
            {
                var result = _userRoleService.Add(entity);

                if (result)
                {
                    return(await Task.FromResult(new BaseResponse <UserRoleDTO>(result)).ConfigureAwait(false));
                }

                return(await Task.FromResult(new BaseResponse <UserRoleDTO>(Message.CreateNotSuccess, false)).ConfigureAwait(false));
            }
            catch (System.Exception)
            {
                return(await Task.FromResult(new BaseResponse <UserRoleDTO>(Message.CreateNotSuccess, false)).ConfigureAwait(false));
            }
        }
        public async Task <MessageModel <string> > UpdateUserInfo(SysUserInfo sysUserInfo)
        {
            var data = new MessageModel <string>();

            if (sysUserInfo == null || sysUserInfo.uID == 0)
            {
                data.success = false;
                data.msg     = "更新用户信息失败!";
                return(data);
            }
            try
            {
                unitOfWork.BeginTran();
                if (sysUserInfo.rIDs.Count > 0)
                {
                    //该用户下所有的角色都删除
                    var userroleids = (await userRoleService.Query(n => n.uID == sysUserInfo.uID)).Select(n => n.ID.ToString()).ToArray();
                    if (userroleids.Count() > 0)
                    {
                        var alluserroleid = await userRoleService.DeleteByIds(userroleids);
                    }
                    //该用户下添加角色
                    var UserRoleAdd = new List <UserRoleInfo>();
                    sysUserInfo.rIDs.ForEach(rid => {
                        UserRoleAdd.Add(new UserRoleInfo(sysUserInfo.uID, rid));
                    });
                    var addUserRoles = await userRoleService.Add(UserRoleAdd);
                }
                sysUserInfo.uUpdateTime = DateTime.Now;
                data.success            = await sysUserService.Update(sysUserInfo);

                unitOfWork.BeginTran();
                if (data.success)
                {
                    data.msg = "更新用户信息成功";
                }
            }
            catch (Exception ex)
            {
                unitOfWork.RollbackTran();
            }
            return(data);
        }
Esempio n. 8
0
        public int AddUserRole(string roleId, string userId)
        {
            var ri = 0;
            var ui = 0;

            if (int.TryParse(roleId, out ri) && int.TryParse(userId, out ui))
            {
                var ur = _userRoleService.Get(g => g.Role.Id == ri && g.User.ID == ui);
                if (ur == null)
                {
                    _userRoleService.Add(new UserRole
                    {
                        Role = _roleService.Get(g => g.Id == ri),
                        User = _userService.Get(g => g.ID == ui)
                    });
                    return(1);
                }
            }
            return(0);
        }
Esempio n. 9
0
        public string updatePermission(int id, int usertemp)
        {
            string msg   = "";
            var    grant = userRoleService.FindById(usertemp, id);

            if (grant == null)
            {
                var userRole = new UserRoleViewModel()
                {
                    UserId = usertemp, RoleId = id
                };
                userRoleService.Add(userRole);
                msg = "<div class='alert alert-success'>Cấp quyền thành công</div>";
            }
            else
            {
                userRoleService.Delete(grant);
                msg = "<div class='alert alert-success'>Hủy quyền thành công</div>";
            }
            userRoleService.Save();
            return(msg);
        }
Esempio n. 10
0
        public async Task <MessageModel <string> > Post(UserRole userRole)
        {
            var data = new MessageModel <string>();

            if (userRole != null)
            {
                userRole.IsDelete   = false;
                userRole.CreateTime = DateTime.Now;
                userRole.ModifyTime = DateTime.Now;
                var result = await _userRoleService.Add(userRole).ConfigureAwait(false);

                data.Success = result > 0;
                if (data.Success)
                {
                    data.Response = result.ObjToString();
                    data.Msg      = "添加成功";
                }
            }

            // TODO:添加日志到数据库
            return(data);
        }
        public ActionResult Register(UserAddOrUpdateViewModel userAddOrUpdateViewModel)
        {
            User user = new User
            {
                Email     = userAddOrUpdateViewModel.User.Email,
                FirstName = userAddOrUpdateViewModel.User.FirstName,
                Hashword  = HashwordHelper.GenerateSHA512String(userAddOrUpdateViewModel.User.Hashword),
                LastName  = userAddOrUpdateViewModel.User.LastName,
                UserName  = userAddOrUpdateViewModel.User.UserName,
            };
            string   userName = userAddOrUpdateViewModel.User.UserName;
            string   password = userAddOrUpdateViewModel.User.Hashword;
            int      userId   = _userService.Add(user).Id;
            UserRole userRole = new UserRole {
                RoleId = 3, UserId = userId
            };

            _userRoleService.Add(userRole);
            ActionResult action = new AccountController(_userService, _userRoleService).Login(userName, password);

            return(action);
        }
        public ActionResult Signup(UserSignupModel model)
        {
            if (ModelState.IsValid)
            {
                var check = userService.CheckAccountExists(model.UserName_S);
                if (check)
                {
                    //var user = Mapper.Map<User>(model); if UserName not S then OK
                    User user = new User();
                    user.FirstName    = model.FirstName;
                    user.LastName     = model.LastName;
                    user.UserName     = model.UserName_S;
                    user.PasswordHash = PasswordHashMD5.MD5Hash(model.Password_S);
                    user.DateOfBirth  = DateTime.Now;
                    user.Gender       = true;
                    user.Avatar       = "AvatarDefault-Male.png";
                    userService.Add(user);

                    var _user    = userService.GetUser(model.UserName_S, PasswordHashMD5.MD5Hash(model.Password_S));
                    var userRole = new UserRole();
                    userRole.UserId = _user.UserId;
                    userRole.RoleId = roleService.GetByName(Common.Role.MEMBER).First().RoleId;
                    userRoleService.Add(userRole);
                    var roles = userRoleService.GetByUserId(_user.UserId);
                    Session[UserSession.UserId]   = _user.UserId;
                    Session[UserSession.FullName] = model.FirstName + " " + model.LastName;
                    Session[UserSession.Avatar]   = _user.Avatar;
                    Session[UserSession.Role]     = roles;
                    return(Redirect("/Home/Index"));
                }
                else
                {
                    TempData["statusSignup"]  = false;
                    TempData["messageSignup"] = "Tên đăng nhập đã tồn tại.Vui lòng kiểm tra lại.";
                }
            }
            return(View("Index"));
        }
        //[Route("Register")]
        public async Task <ResponseMessage <long> > Post([FromBody] RegisterUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                long id = await _usersService.CheckAdd(model.UserName, model.Password, model.Mail, model.Phone);

                if (id != -1)
                {
                    await _userRoleService.Add(new UserRole()
                    {
                        UserId = id,
                        RoleId = 4
                    });

                    return(new ResponseMessage <long>()
                    {
                        Msg = "注册成功",
                        Status = 200,
                        Success = true,
                        ResponseInfo = id
                    });
                }
                else
                {
                    return(new ResponseMessage <long>()
                    {
                        Msg = "注册失败,当前用户名已存在",
                        Success = false,
                    });
                }
            }
            return(new ResponseMessage <long>()
            {
                Msg = "注册失败,校验不通过",
                Success = false
            });
        }