Exemplo n.º 1
0
        public Task <UsersModelLite> GetAsync(int id)
        {
            try
            {
                var dal = _usersRepository.Get(id);

                if (dal == null)
                {
                    return(Task.FromResult <UsersModelLite>(null));
                }
                else
                {
                    var model = new UsersModelLite()
                    {
                        Id                   = dal.Id,
                        InsertedBy           = dal.InsertedBy,
                        InsertedDate         = dal.InsertedDate,
                        UpdatedBy            = dal.UpdatedBy,
                        UpdatedDate          = dal.UpdatedDate,
                        Email                = dal.Email,
                        EmailConfirmed       = dal.EmailConfirmed,
                        PhoneNumber          = dal.PhoneNumber,
                        PhoneNumberConfirmed = dal.PhoneNumberConfirmed,
                        TwoFactorEnabled     = dal.TwoFactorEnabled,
                        LockoutEndDate       = dal.LockoutEndDate,
                        LockoutEnabled       = dal.LockoutEnabled,
                        AccessFailedCount    = dal.AccessFailedCount,
                        UserName             = dal.UserName,
                        Roles                = dal.Roles.Select(r => r.Id).ToList()
                    };

                    //UsersModel model = new UsersModel();
                    //ReducedAutoMapper.Instance.CreateMap<Users, UsersModel>();
                    //ReducedAutoMapper.Instance.CreateMap<Roles, RolesModel>();
                    //ReducedAutoMapper.Instance.CreateMap<Roles, RolesModel>();
                    //model = ReducedAutoMapper.Instance.Map<Users, UsersModel>(dal as Users);
                    //UsersModel model = AutoMapperGenericHelper<Users, UsersModel>.Convert(dal);

                    return(Task.FromResult(model));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        private bool hasPermission(HttpActionContext actionContext)
        {
            try
            {
                string controllerName = actionContext.ControllerContext.ControllerDescriptor.ControllerType.Name;
                string actionName     = actionContext.ActionDescriptor.ActionName;
                string userId         = string.Empty;
                string userName       = string.Empty;

                if (actionContext.Request.Properties.ContainsKey("MS_OwinContext"))
                {
                    var context        = (OwinContext)actionContext.Request.Properties["MS_OwinContext"];
                    var claimsIdentity = context.Authentication.User.Identity as ClaimsIdentity;

                    if (claimsIdentity.Claims.ToList().Count > 0)
                    {
                        Claim claimsUserId   = claimsIdentity.Claims.Where(s => s.Type == ClaimTypes.PrimarySid).FirstOrDefault();
                        Claim claimsUserName = claimsIdentity.Claims.Where(s => s.Type == ClaimTypes.Name).FirstOrDefault();
                        userId   = claimsUserId != null ? claimsUserId.Value : string.Empty;
                        userName = claimsUserName != null ? claimsUserName.Value : string.Empty;
                    }

                    var usersService              = WebApiInstaller.Resolve <IUsersService>();
                    var roleInPagesService        = WebApiInstaller.Resolve <IRoleInPagesService>();
                    var controllerInActionService = WebApiInstaller.Resolve <IControllerActionsService>();

                    if (string.IsNullOrEmpty(userId))
                    {
                        return(false);
                    }

                    UsersModelLite user = usersService.GetAsync(int.Parse(userId)).Result;

                    if (user == null)
                    {
                        return(false);
                    }

                    if (user.Roles.Count < 1)
                    {
                        return(false);
                    }

                    var controllerInActions = controllerInActionService.GetControllerActionsByControllerAndActionAsync(controllerName, actionName).Result;

                    if (controllerInActions == null)
                    {
                        return(false);
                    }

                    List <RoleInPagesModel> roleInPageList = new List <RoleInPagesModel>();
                    foreach (int roleId in user.Roles)
                    {
                        var result = roleInPagesService.GetByRoleIdAsync(roleId).Result;

                        if (result != null)
                        {
                            roleInPageList.AddRange(result);
                        }
                    }

                    foreach (RoleInPagesModel model in roleInPageList.Distinct())
                    {
                        switch (controllerInActions.OperationType)
                        {
                        case OperationType.Create:
                            if (model.Create)
                            {
                                return(true);
                            }
                            break;

                        case OperationType.Read:
                            if (model.Read)
                            {
                                return(true);
                            }
                            break;

                        case OperationType.Update:
                            if (model.Update)
                            {
                                return(true);
                            }
                            break;

                        case OperationType.Delete:
                            if (model.Delete)
                            {
                                return(true);
                            }
                            break;

                        case OperationType.Operation:
                            if (model.Create && model.Read && model.Update && model.Delete)
                            {
                                return(true);
                            }
                            break;

                        default:
                            break;
                        }
                    }

                    // var user = usersService.GetAsync(int.Parse(userId)).Result;

                    //var roleList = user.Roles.Where(
                    //    u => u.Pages.Where(
                    //            p => p.ControllerActions.Where(
                    //                c => c.Controller == controllerName && c.Action == actionName).Count() > 0).Count() > 0).ToList();

                    //if (roleList.Count > 0)
                    //    return true;
                    //else
                    //    return false;
                }

                return(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }