Exemplo n.º 1
0
        public async Task <IActionResult> r3UpdateDataModel(UserGroupRole userGroupRole)
        {
            string       PasswordEn = Helper.Encrypt(userGroupRole.sys_Dm_User.Username, userGroupRole.sys_Dm_User.Password);
            RequestToken token      = CommonData.GetDataFromToken(User);

            userGroupRole.sys_Dm_User.UserCreateId = token.UserID;
            userGroupRole.sys_Dm_User.CreateDate   = DateTime.Now;
            // update user
            var user = await _context.Sys_Dm_User.FindAsync(userGroupRole.sys_Dm_User.Id);

            user.Password = PasswordEn;
            user.FullName = userGroupRole.sys_Dm_User.FullName;
            user.Role     = userGroupRole.sys_Dm_User.Role;
            // update quyền
            var userGroup = _context.Sys_Cog_UsersGroup.Where(x => x.UserId == userGroupRole.sys_Dm_User.Id);

            _context.Sys_Cog_UsersGroup.RemoveRange(userGroup);
            foreach (var item in userGroupRole.GroupRoles)
            {
                Sys_Cog_UsersGroup obj = new Sys_Cog_UsersGroup();
                obj.UserId      = user.Id;
                obj.GroupRoleId = item.Id;
                _context.Sys_Cog_UsersGroup.Add(obj);
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(new ObjectResult(new { error = 1 }));
            }

            return(new ObjectResult(new { error = 0 }));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <Sys_Dm_User> > > r1AddDataSysDmUser(UserGroupRole userGroupRole)
        {
            try
            {
                RequestToken token     = CommonData.GetDataFromToken(User);
                var          userLogin = await _context.Sys_Dm_User.FindAsync(token.UserID);

                if (_context.Sys_Dm_User.Count(x => x.Code == userGroupRole.sys_Dm_User.Code) > 0)
                {
                    return(new ObjectResult(new { error = 2, ms = "Mã nhân viên đã tồn tại trong hệ thống." }));
                }
                var department = await _context.Sys_Dm_Department.FindAsync(userLogin.DepartmentId);

                var nest = await _context.Sys_Dm_Department.FindAsync(userGroupRole.sys_Dm_User.NestId);

                Nullable <int> NestId = null;
                if (nest != null)
                {
                    NestId = nest.Id;
                }
                var position = await _context.Sys_Dm_Position.FindAsync(userGroupRole.sys_Dm_User.PositionId);

                if (position == null)
                {
                    return(new ObjectResult(new { error = 2, ms = "Chưa chọn chức vụ cho nhân viên mới." }));
                }
                string PasswordEn = Helper.Encrypt(userGroupRole.sys_Dm_User.Username, userGroupRole.sys_Dm_User.Password);
                userGroupRole.sys_Dm_User.Password       = PasswordEn;
                userGroupRole.sys_Dm_User.PositionName   = position.Name;
                userGroupRole.sys_Dm_User.DepartmentName = department.Name;
                userGroupRole.sys_Dm_User.UserCreateId   = token.UserID;
                userGroupRole.sys_Dm_User.CreateDate     = DateTime.Now;
                userGroupRole.sys_Dm_User.NestId         = NestId;
                _context.Sys_Dm_User.Add(userGroupRole.sys_Dm_User);
                await _context.SaveChangesAsync();

                var user = _context.Sys_Dm_User.FirstOrDefault(x => x.Code == userGroupRole.sys_Dm_User.Code);
                foreach (var item in userGroupRole.GroupRoles)
                {
                    Sys_Cog_UsersGroup obj = new Sys_Cog_UsersGroup();
                    obj.UserId      = user.Id;
                    obj.GroupRoleId = item.Id;
                    _context.Sys_Cog_UsersGroup.Add(obj);
                }
                await _context.SaveChangesAsync();

                return(new ObjectResult(new { error = 0 }));
            }
            catch (Exception)
            {
                return(new ObjectResult(new { error = 1 }));
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult <IEnumerable <Sys_Dm_User> > > r1AddDataSysDmUser(UserGroupRole userGroupRole)
        {
            try
            {
                var userId    = Convert.ToInt32(User.Claims.First(c => c.Type == "UserId").Value);
                var userLogin = await _context.Sys_Dm_User.FindAsync(userId);

                if (_context.Sys_Dm_User.Count(x => x.Code == userGroupRole.sys_Dm_User.Code) > 0)
                {
                    return(new ObjectResult(new { error = 2, ms = "Mã nhân viên đã tồn tại trong hệ thống." }));
                }
                var department = await _context.Sys_Dm_Department.FindAsync(userLogin.ParentDepartId);

                var position = await _context.Sys_Dm_Position.FindAsync(userGroupRole.sys_Dm_User.PositionId);

                if (position == null)
                {
                    return(new ObjectResult(new { error = 2, ms = "Chưa chọn chức vụ cho nhân viên mới." }));
                }
                string PasswordEn = Helper.Encrypt(userGroupRole.sys_Dm_User.Username, userGroupRole.sys_Dm_User.Password);
                userGroupRole.sys_Dm_User.Password       = PasswordEn;
                userGroupRole.sys_Dm_User.PositionName   = position.Name;
                userGroupRole.sys_Dm_User.DepartmentName = department.Name;
                userGroupRole.sys_Dm_User.UserCreateId   = userId;
                userGroupRole.sys_Dm_User.CreateDate     = DateTime.Now;
                _context.Sys_Dm_User.Add(userGroupRole.sys_Dm_User);
                await _context.SaveChangesAsync();

                var user = _context.Sys_Dm_User.FirstOrDefault(x => x.Code == userGroupRole.sys_Dm_User.Code);
                foreach (var item in userGroupRole.GroupRoles)
                {
                    Sys_Cog_UsersGroup obj = new Sys_Cog_UsersGroup();
                    obj.UserId      = user.Id;
                    obj.GroupRoleId = item.Id;
                    _context.Sys_Cog_UsersGroup.Add(obj);
                }
                await _context.SaveChangesAsync();

                return(new ObjectResult(new { error = 0 }));
            }
            catch (Exception)
            {
                return(new ObjectResult(new { error = 1 }));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> r3UpdateDataModel(int id, UserGroupRole userGroupRole)
        {
            string PasswordEn = Helper.Encrypt(userGroupRole.sys_Dm_User.Username, userGroupRole.sys_Dm_User.Password);
            var    userId     = Convert.ToInt32(User.Claims.First(c => c.Type == "UserId").Value);

            if (id != userGroupRole.sys_Dm_User.Id)
            {
                return(new ObjectResult(new { error = 1 }));
            }
            userGroupRole.sys_Dm_User.UserCreateId = userId;
            userGroupRole.sys_Dm_User.CreateDate   = DateTime.Now;
            // update user
            var user = await _context.Sys_Dm_User.FindAsync(userGroupRole.sys_Dm_User.Id);

            user.Password = PasswordEn;
            user.FullName = userGroupRole.sys_Dm_User.FullName;
            user.Code     = userGroupRole.sys_Dm_User.Code;
            // update quyền
            var userGroup = _context.Sys_Cog_UsersGroup.Where(x => x.UserId == userGroupRole.sys_Dm_User.Id);

            _context.Sys_Cog_UsersGroup.RemoveRange(userGroup);
            foreach (var item in userGroupRole.GroupRoles)
            {
                Sys_Cog_UsersGroup obj = new Sys_Cog_UsersGroup();
                obj.UserId      = user.Id;
                obj.GroupRoleId = item.Id;
                _context.Sys_Cog_UsersGroup.Add(obj);
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(new ObjectResult(new { error = 1 }));
            }

            return(new ObjectResult(new { error = 0 }));
        }