Exemplo n.º 1
0
        public void CreateRoles()
        {
            if (foundAudience == null)
            {
                CreateAudiences();
            }

            /*
             * create test roles
             */

            foundRole = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <tbl_Role>()
                                       .Where(x => x.Name == TestDefaultConstants.RoleName).ToLambda())
                        .SingleOrDefault();

            if (foundRole == null)
            {
                foundRole = _uow.Roles.Create(
                    _map.Map <tbl_Role>(new RoleV1()
                {
                    AudienceId  = foundAudience.Id,
                    Name        = TestDefaultConstants.RoleName,
                    IsEnabled   = true,
                    IsDeletable = true,
                }));

                _uow.Commit();
            }
        }
Exemplo n.º 2
0
        public bool IsInRole(tbl_User user, tbl_Role role)
        {
            if (_context.Set<tbl_UserRole>()
                .Any(x => x.UserId == user.Id && x.RoleId == role.Id))
                return true;

            return false;
        }
Exemplo n.º 3
0
        public bool IsInRole(tbl_Audience audience, tbl_Role role)
        {
            if (_context.Set <tbl_AudienceRole>()
                .Any(x => x.AudienceId == audience.Id && x.RoleId == role.Id))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 4
0
        public ActionResult Manage(RoleInfo info)
        {
            var service = Container.GetService <IRoleService>();
            var data    = new AjaxResult();

            try
            {
                if (info.Key > 0)
                {
                    var role = service.GetModels(r => r.keyid == info.Key).FirstOrDefault();
                    if (role != null)
                    {
                        role.C_Name        = info.Name;
                        role.C_Description = info.Description;
                        role.C_ParentRole  = info.ParentKey;
                        role.C_UpdatedDate = DateTime.Now;
                        var success = service.Update(role);
                        if (success)
                        {
                            data.Message = "修改成功";
                        }
                        else
                        {
                            data.Message = "修改失败";
                        }
                    }
                    else
                    {
                        data.Message = "不存在的角色";
                    }
                }
                else
                {
                    var role = new tbl_Role();
                    role.C_Name        = info.Name;
                    role.C_Description = info.Description;
                    role.C_ParentRole  = info.ParentKey;
                    role.C_CreatedDate = DateTime.Now;
                    var success = service.Add(role);

                    if (success)
                    {
                        data.Message = "添加成功";
                    }
                    else
                    {
                        data.Message = "添加失败";
                    }
                }
            }
            catch (Exception ex) {
                data.Message = ex.Message;
            }
            return(Json(data));
        }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> AddRole(tbl_Role item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CRUDOperation operation = new CRUDOperation();
            tbl_Role      dbitem    = operation.AddRole(item);

            return(Ok(dbitem));
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> UpdateRole(tbl_Role item)
        {
            CRUDOperation operation = new CRUDOperation();

            if (item == null)
            {
                return(NotFound());
            }
            else
            {
                var dbitem = operation.UpdateRole(item);
                return(Ok(dbitem));
            }
        }
Exemplo n.º 7
0
        public IActionResult GetV1([FromRoute] string roleValue)
        {
            Guid     roleID;
            tbl_Role role = null;

            if (Guid.TryParse(roleValue, out roleID))
            {
                role = uow.Roles.Get(x => x.Id == roleID)
                       .SingleOrDefault();
            }
            else
            {
                role = uow.Roles.Get(x => x.Name == roleValue)
                       .SingleOrDefault();
            }

            if (role == null)
            {
                ModelState.AddModelError(MessageType.RoleNotFound.ToString(), $"Role:{roleValue}");
                return(NotFound(ModelState));
            }

            return(Ok(map.Map <RoleV1>(role)));
        }
Exemplo n.º 8
0
 public void update(tbl_Role item)
 {
     rep.update(item);
 }
Exemplo n.º 9
0
 public void add(tbl_Role item)
 {
     rep.add(item);
 }
Exemplo n.º 10
0
        public void CreateRoles()
        {
            if (foundAudience_Alert == null ||
                foundAudience_Identity == null)
            {
                CreateAudiences();
            }

            /*
             * create default roles
             */

            foundRoleForAdmin_Alert = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <tbl_Role>()
                                                     .Where(x => x.Name == DefaultConstants.RoleForAdmins_Alert).ToLambda())
                                      .SingleOrDefault();

            if (foundRoleForAdmin_Alert == null)
            {
                foundRoleForAdmin_Alert = _uow.Roles.Create(
                    _map.Map <tbl_Role>(new RoleV1()
                {
                    AudienceId  = foundAudience_Alert.Id,
                    Name        = DefaultConstants.RoleForAdmins_Alert,
                    IsEnabled   = true,
                    IsDeletable = false,
                }));

                _uow.Commit();
            }

            foundRoleForUser_Alert = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <tbl_Role>()
                                                    .Where(x => x.Name == DefaultConstants.RoleForUsers_Alert).ToLambda())
                                     .SingleOrDefault();

            if (foundRoleForUser_Alert == null)
            {
                foundRoleForUser_Alert = _uow.Roles.Create(
                    _map.Map <tbl_Role>(new RoleV1()
                {
                    AudienceId  = foundAudience_Alert.Id,
                    Name        = DefaultConstants.RoleForUsers_Alert,
                    IsEnabled   = true,
                    IsDeletable = false,
                }));

                _uow.Commit();
            }

            foundRoleForAdmin_Identity = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <tbl_Role>()
                                                        .Where(x => x.Name == DefaultConstants.RoleForAdmins_Identity).ToLambda())
                                         .SingleOrDefault();

            if (foundRoleForAdmin_Identity == null)
            {
                foundRoleForAdmin_Identity = _uow.Roles.Create(
                    _map.Map <tbl_Role>(new RoleV1()
                {
                    AudienceId  = foundAudience_Identity.Id,
                    Name        = DefaultConstants.RoleForAdmins_Identity,
                    IsEnabled   = true,
                    IsDeletable = false,
                }));

                _uow.Commit();
            }

            foundRoleForUser_Identity = _uow.Roles.Get(QueryExpressionFactory.GetQueryExpression <tbl_Role>()
                                                       .Where(x => x.Name == DefaultConstants.RoleForUsers_Identity).ToLambda())
                                        .SingleOrDefault();

            if (foundRoleForUser_Identity == null)
            {
                foundRoleForUser_Identity = _uow.Roles.Create(
                    _map.Map <tbl_Role>(new RoleV1()
                {
                    AudienceId  = foundAudience_Identity.Id,
                    Name        = DefaultConstants.RoleForUsers_Identity,
                    IsEnabled   = true,
                    IsDeletable = false,
                }));

                _uow.Commit();
            }
        }
Exemplo n.º 11
0
        public ActionResult CreateUser(tbl_User tbl_User, tbl_Role tbl_Role)
        {
            using (var _context = new Entities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        string password = string.Empty;
                        if (tbl_User.Pno != null)
                        {
                            password = "******" + tbl_User.Pno.Trim();
                        }
                        if (!ModelState.IsValid)
                        {
                            UserRoleViewModel vm = new UserRoleViewModel()
                            {
                                _tbl_Role = _context.tbl_Role.ToList(),
                                _tbl_Unit = _context.tbl_Unit.ToList(),
                            };
                            return(PartialView("_CreateUser", vm));
                        }

                        if (tbl_User.Pno != null)
                        {
                            tbl_User.UserId   = tbl_User.Pno;
                            tbl_User.Password = password;

                            _context.tbl_User.Add(tbl_User);
                            _context.SaveChanges();

                            if (tbl_Role.RoleId != null)
                            {
                                var obj = new tbl_UserRole()
                                {
                                    UserId    = tbl_User.UserId,
                                    RoleId    = tbl_Role.RoleId,
                                    IsDefault = 1,
                                    IsActive  = 1,
                                };
                                _context.tbl_UserRole.Add(obj);
                                _context.SaveChanges();
                            }
                        }


                        #region User Injection

                        var UserName = tbl_User.UserId;
                        var Password = password;


                        var objNewAdminUser = new ApplicationUser {
                            Id = tbl_User.UserId, UserName = tbl_User.UserId, Email = tbl_User.UserId
                        };
                        var AdminUserCreateResult = UserManager.Create(objNewAdminUser, Password);
                        if (AdminUserCreateResult.Succeeded)
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }

                        #endregion

                        return(RedirectToAction("UserIndex"));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        //   Exception(ex);
                        // Alert("Their is something went wrong!!!", NotificationType.error);
                        return(RedirectToAction("UserIndex"));
                    }
                }
            }
        }
Exemplo n.º 12
0
        public ActionResult Edit(string id, tbl_Role tbl_Role, string selectedItems)
        {
            using (var _context = new Entities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    List <TreeViewNode> parentItems = new List <TreeViewNode>();
                    var vm = new RolesViewModel();
                    try
                    {
                        if (id != null)
                        {
                            _context.tbl_RolePermission.RemoveRange(_context.tbl_RolePermission.Where(x => x.RoleId == id));
                            _context.SaveChanges();
                        }
                        if (selectedItems != "[]")
                        {
                            List <TreeViewNode> items           = (new JavaScriptSerializer()).Deserialize <List <TreeViewNode> >(selectedItems);
                            List <string>       newListOfParent = new List <string>();
                            foreach (var temp in items.Select(x => x.parents))
                            {
                                newListOfParent = newListOfParent.Concat(temp).ToList();
                            }
                            newListOfParent = newListOfParent.Distinct().ToList();

                            foreach (var selected in items)
                            {
                                if (selected.parent == "#")
                                {
                                    selected.parent = "0";
                                }

                                if (!_context.tbl_RolePermission.Where(x => x.RoleId == tbl_Role.RoleId).Any(x => x.PermissionId == selected.id))
                                {
                                    var model = new tbl_RolePermission
                                    {
                                        PermissionId = selected.id,
                                        RoleId       = tbl_Role.RoleId,
                                    };
                                    _context.tbl_RolePermission.Add(model);
                                }
                            }

                            foreach (var selected in newListOfParent)
                            {
                                if (!items.Where(x => x.id == selected).Any() && selected != "#")
                                {
                                    tbl_Permission permission = _context.tbl_Permission.Where(x => x.PermissionId == selected).FirstOrDefault();

                                    if (!_context.tbl_RolePermission.Where(x => x.RoleId == tbl_Role.RoleId).Any(x => x.PermissionId == selected))
                                    {
                                        var model = new tbl_RolePermission
                                        {
                                            PermissionId = selected,
                                            RoleId       = tbl_Role.RoleId,
                                        };
                                        _context.tbl_RolePermission.Add(model);
                                    }
                                }
                            }

                            _context.SaveChanges();
                        }


                        //Serialize to JSON string.
                        // Alert("Data Saved Sucessfully!!!", NotificationType.success);
                        transaction.Commit();
                        return(RedirectToAction("ViewRoles"));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        //   Exception(ex);
                        //  Alert("Their is something went wrong!!!", NotificationType.error);
                        return(RedirectToAction("ViewRoles"));
                    }
                }
            }
        }