public IActionResult RemoveAuthRole([FromBody] JObject request)
 {
     try {
         User currentUser = _userService.CheckAdmin(_httpContextAccessor.HttpContext.User);
         if (currentUser != null)
         {
             UserRoleModel role = _context.Roles.FirstOrDefault(r => r.Roll_Id == currentUser.Roll_Id);
             if (role.Rollname == "admin")
             {
                 return(Ok(SendResult.SendError("Can not Change admin role.")));
             }
             int           id    = request["id"].Value <int>();
             AuthRoleModel model = _context.AuthRoles.FirstOrDefault(f => f.id == id);
             _context.AuthRoles.Remove(model);
             _context.SaveChanges();
             _eventService.SaveEvent(currentUser.User_Id, EventUserLog.EVENT_REMOVE, "AuthRole");
             return(Ok(true));
         }
         else
         {
             return(Ok(SendResult.SendError("You must login.")));
         }
     } catch (Exception error) {
         Console.WriteLine(error);
         return(BadRequest(SendResult.SendError("You don`t create new complany")));
     }
 }
 public IActionResult UpdateAuthRole([FromBody] AuthRoleModel request)
 {
     try {
         User currentUser = _userService.CheckAdmin(_httpContextAccessor.HttpContext.User);
         if (currentUser != null)
         {
             AuthRoleModel model = _context.AuthRoles.FirstOrDefault(f => f.id == request.id);
             UserRoleModel role  = _context.Roles.FirstOrDefault(r => r.Roll_Id == model.roll_id);
             if (role.Rollname == "admin")
             {
                 return(Ok(SendResult.SendError("Can not Change admin role.")));
             }
             model.menu       = request.menu;
             model.saveRole   = request.saveRole;
             model.deleteRole = request.deleteRole;
             model.updateRole = request.updateRole;
             model.viewRole   = request.viewRole;
             model.printRole  = request.printRole;
             model.status     = request.status;
             _context.SaveChanges();
             _eventService.SaveEvent(currentUser.User_Id, EventUserLog.EVENT_UPDATE, "AuthRole");
             return(Ok(model));
         }
         else
         {
             return(Ok(SendResult.SendError("You must login.")));
         }
     } catch (Exception error) {
         Console.WriteLine(error);
         return(BadRequest(SendResult.SendError("You don`t create new complany")));
     }
 }
Beispiel #3
0
        public async Task <IHttpActionResult> PostCheck([FromBody] AuthRoleModel roleModel)
        {
            Token token = new Token();

            try
            {
                token = await db.Tokens.FirstOrDefaultAsync(x => x.AccessToken == roleModel.Token);
            }
            catch
            {
                return(Unauthorized());
            }

            if (token == null)
            {
                return(Unauthorized());
            }

            if (token.TimeOfReleaseAccessToken <= DateTime.Now)
            {
                return(Unauthorized());
            }

            token.TimeOfReleaseAccessToken  = DateTime.Now.AddMinutes(30);
            token.TimeOfReleaseRefreshToken = DateTime.Now.AddMinutes(30);
            db.Entry(token).State           = EntityState.Modified;
            try
            {
                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(InternalServerError());
            }


            User user = new User();

            try
            {
                user = await db.Users.FindAsync(token.UserId);
            }
            catch
            {
                return(Unauthorized());
            }

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

            if (user.UserRole != roleModel.RequiredRole)
            {
                return(Content(HttpStatusCode.Unauthorized, "Sorry. No access rights. Authenticate as admin"));
            }

            return(Ok());
        }
Beispiel #4
0
        public User CheckUseRole(ClaimsPrincipal currentUser, string menu, string function)
        {
            try {
                if (currentUser.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    var user = GetUserByEmail(currentUser.FindFirst(ClaimTypes.Email).Value);
                    if (user != null)
                    {
                        AuthRoleModel roles = _context.AuthRoles.FirstOrDefault(a => a.roll_id == user.Roll_Id && menu == a.menu);
                        switch (function)
                        {
                        case "create":
                            if (roles.saveRole)
                            {
                                return(user);
                            }
                            break;

                        case "read":
                            if (roles.viewRole)
                            {
                                return(user);
                            }
                            break;

                        case "update":
                            if (roles.updateRole)
                            {
                                return(user);
                            }
                            break;

                        case "delete":
                            if (roles.deleteRole)
                            {
                                return(user);
                            }
                            break;

                        default:
                            return(null);
                        }
                        return(null);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            } catch (Exception ex) {
                Console.Write(ex.ToString());
                return(null);
            }
        }
 public async Task Returns_ClaimsPrincipal_With_Role_Claims()
 {
     // Arrange
     var role0 = new AuthRoleModel(LongId <AuthRoleId>(), Rnd.Str);
     var role1 = new AuthRoleModel(LongId <AuthRoleId>(), Rnd.Str);
     var user  = new AuthUserModel
     {
         Id           = LongId <AuthUserId>(),
         EmailAddress = Rnd.Str,
         FriendlyName = Rnd.Str,
         IsSuper      = true,
         Roles        = new() { { role0 }, { role1 } }
     };
 public IActionResult GetAuthRole([FromBody] JObject request)
 {
     try {
         User currentUser = _userService.GetCurrentUser(_httpContextAccessor.HttpContext.User);
         if (currentUser != null)
         {
             int           id    = request["id"].Value <int>();
             AuthRoleModel model = _context.AuthRoles.FirstOrDefault(f => f.id == id);
             return(Ok(model));
         }
         else
         {
             return(Ok(SendResult.SendError("You must login.")));
         }
     } catch (Exception error) {
         Console.WriteLine(error);
         return(BadRequest(SendResult.SendError("You don`t create new complany")));
     }
 }
 public IActionResult CreateMenuRole([FromBody] AuthRoleModel request)
 {
     try {
         User currentUser = _userService.CheckAdmin(_httpContextAccessor.HttpContext.User);
         if (currentUser != null)
         {
             request.created_by   = currentUser.User_Id;
             request.created_date = DateTime.Now;
             _context.Add(request);
             _context.SaveChanges();
             _eventService.SaveEvent(currentUser.User_Id, EventUserLog.EVENT_CREATE, "AuthRole");
             return(Ok(request));
         }
         else
         {
             return(Ok(SendResult.SendError("You must login.")));
         }
     } catch (Exception error) {
         Console.WriteLine(error);
         return(BadRequest(SendResult.SendError("You don`t create new complany")));
     }
 }
Beispiel #8
0
        public async Task <IActionResult> AuthRole(UrlParameter param, AuthRoleModel model)
        {
            var result = await _userService.AddRoles(model.UserId, model.RoleIds);

            return(Json(result));
        }
Beispiel #9
0
        public async Task <IActionResult> DeleteRole(AuthRoleModel model)
        {
            var result = await _userService.RemoveRoles(model.UserId, model.RoleIds);

            return(Json(result));
        }