示例#1
0
        public IActionResult GetAll()
        {
            // List of messages to return to the client
            var messages = new List <Message>();

            // Authentication
            var controllerHelper = new ControllerHelper(context);
            var userModel        = controllerHelper.Authenticate(HttpContext.User.Identity as ClaimsIdentity);

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

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(userModel, (long)SystemDatasetsEnum.Rights, RightsEnum.R))
            {
                return(Forbid());
            }

            // Get data from database
            var rightsRepository = new RightsRepository(context);
            var rightsModelList  = rightsRepository.GetAllByApplicationId(userModel.ApplicationId);

            // Remove unnecessary data
            foreach (var item in rightsModelList)
            {
                item.Application = null;
                item.Users       = null;
            }

            return(Ok(rightsModelList));
        }
示例#2
0
 public ActionResult MenuListFilter(int roleId)
 {
     using (var db = new RightsRepository())
     {
         var getComData = db.GetRightsByRoleId(roleId);
         return(Json(getComData, JsonRequestBehavior.AllowGet));
     }
 }
示例#3
0
        public RightsServiceTest()
        {
            IUnitOfWork       uow = new NHUnitOfWork();
            IRightsRepository _rightsRepository = new RightsRepository(uow);

            this._rightsService = new RightsService(_rightsRepository, uow);
            AutoMapperBootStrapper.ConfigureAutoMapper();
        }
示例#4
0
 public ActionResult Rights()
 {
     if (!string.IsNullOrEmpty(Convert.ToString(Session["UserId"])))
     {
         using (var db = new RightsRepository())
         {
             Session["RightEdit"] = "";
             //var model = db.GetRightsDetails(null);
             List <DisplayMenuModel> model = rights.DisplayMenuList();
             RoleList(null);
             return(View(model));
         }
     }
     return(RedirectToAction("Login", "Login"));
 }
示例#5
0
        public ActionResult AddRights(int roleId, string[] menuData)
        {
            using (var db = new HospitalTransparencyEntities())
            {
                #region Remove rights first from table
                db.Database.ExecuteSqlCommand("Delete from RightsMaster where RoleId = '" + roleId + "'");
                #endregion

                #region If rights are not for Super Admin then Update Rights for all users for that particular role

                foreach (var item in menuData)
                {
                    var  menus   = item.Split(',');
                    var  menuId  = menus[0];
                    bool?add     = Convert.ToBoolean(menus[1]);
                    bool?edit    = Convert.ToBoolean(menus[2]);
                    bool?delete  = Convert.ToBoolean(menus[3]);
                    bool?display = Convert.ToBoolean(menus[4]);

                    var right = new RightsModel();
                    right.MenuId      = int.Parse(menuId);
                    right.RoleId      = roleId;
                    right.Add         = add;
                    right.Edit        = edit;
                    right.Display     = display;
                    right.Delete      = delete;
                    right.ClientId    = null;
                    right.UserId      = null;
                    right.CreatedBy   = Convert.ToInt32(System.Web.HttpContext.Current.Session["UserId"]);
                    right.CreatedDate = DateTime.Now;
                    using (var context = new RightsRepository())
                    {
                        context.AddRights(right);
                    }
                }

                CustomCacheManagement.MenuList = new RightsListRepository().GetMenuList();
                #endregion

                return(Json(true, JsonRequestBehavior.AllowGet));
            }
        }
示例#6
0
        public IActionResult GetById(long id)
        {
            // List of messages
            var messages = new List <Message>();

            // Authentication
            var controllerHelper = new ControllerHelper(context);
            var authUserModel    = controllerHelper.Authenticate(HttpContext.User.Identity as ClaimsIdentity);

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

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(authUserModel, (long)SystemDatasetsEnum.Rights, RightsEnum.R))
            {
                return(Forbid());
            }

            // Get data from database
            var rightsRepository = new RightsRepository(context);
            var rightsModel      = rightsRepository.GetById(authUserModel.ApplicationId, id);

            if (rightsModel == null)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         4006,
                                         new List <string>()
                {
                    id.ToString()
                }));
                return(BadRequest(messages));
            }

            // Remove unnecessary data
            rightsModel.Application = null;
            rightsModel.Users       = null;

            return(Ok(rightsModel));
        }
示例#7
0
        public IActionResult Put([FromBody] RightsModel fromBodyRightsModel)
        {
            // List of messages to return to the client
            var messages = new List <Message>();

            // Authentication
            var controllerHelper = new ControllerHelper(context);
            var authUserModel    = controllerHelper.Authenticate(HttpContext.User.Identity as ClaimsIdentity);

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

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(authUserModel, (long)SystemDatasetsEnum.Rights, RightsEnum.CRU))
            {
                return(Forbid());
            }

            #region VALIDATIONS

            // Received rights ApplicationId must be the same as of authorized user
            var sharedValidationHelper = new SharedValidationHelper();
            messages = sharedValidationHelper.ValidateApplicationId(fromBodyRightsModel.ApplicationId, authUserModel.ApplicationId);
            if (messages.Count != 0)
            {
                return(BadRequest(messages));
            }
            fromBodyRightsModel.Application = authUserModel.Application;

            // Rights must already exist in the database
            var rightsRepository = new RightsRepository(context);
            var rightsModel      = rightsRepository.GetById(authUserModel.ApplicationId, fromBodyRightsModel.Id);
            if (rightsModel == null)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         4003,
                                         new List <string>()
                {
                    fromBodyRightsModel.Application.LoginApplicationName,
                    fromBodyRightsModel.Id.ToString()
                }));
                Logger.LogMessagesToConsole(messages);
                return(BadRequest(messages));
            }

            // If the rights name was changed, the new one must be unique
            if (rightsModel.Name != fromBodyRightsModel.Name)
            {
                var sameNameRights = rightsRepository.GetByApplicationIdAndName(authUserModel.ApplicationId, fromBodyRightsModel.Name);
                if (sameNameRights.Count() > 0)
                {
                    messages.Add(new Message(MessageTypeEnum.Error,
                                             4001,
                                             new List <string>()
                    {
                        fromBodyRightsModel.Name
                    }));
                    return(BadRequest(messages));
                }
            }

            // Rights data validity and logic validity
            messages = sharedValidationHelper.ValidateRights(authUserModel.Application.ApplicationDescriptor,
                                                             fromBodyRightsModel.DataDictionary);
            if (messages.Count != 0)
            {
                return(BadRequest(messages));
            }

            #endregion

            rightsRepository.SetNameAndData(rightsModel, fromBodyRightsModel.Name, fromBodyRightsModel.DataDictionary);
            messages.Add(new Message(MessageTypeEnum.Info,
                                     4007,
                                     new List <string>()
            {
                fromBodyRightsModel.Name
            }));
            return(Ok(messages));
        }
示例#8
0
        public IActionResult DeleteById(long id)
        {
            // List of messages to return to the client
            var messages = new List <Message>();

            // Authentication
            var controllerHelper = new ControllerHelper(context);
            var authUserModel    = controllerHelper.Authenticate(HttpContext.User.Identity as ClaimsIdentity);

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

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(authUserModel, (long)SystemDatasetsEnum.Rights, RightsEnum.CRUD))
            {
                return(Forbid());
            }

            #region VALIDATIONS

            // Rights must already exist in the database
            var rightsRepository = new RightsRepository(context);
            var rightsModel      = rightsRepository.GetById(authUserModel.ApplicationId, id);
            if (rightsModel == null)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         4003,
                                         new List <string>()
                {
                    rightsModel.Application.LoginApplicationName,
                    rightsModel.Id.ToString()
                }));
                Logger.LogMessagesToConsole(messages);
                return(BadRequest(messages));
            }

            // Check if no users are using rights to delete
            var userRepository = new UserRepository(context);
            var users          = userRepository.GetByRightsId(rightsModel.Id);
            if (users.Count() > 0)
            {
                var usernames = String.Join(", ", users.Select(u => u.GetUsername()));
                messages.Add(new Message(MessageTypeEnum.Error,
                                         4004,
                                         new List <string>()
                {
                    rightsModel.Name,
                    usernames
                }));
                return(BadRequest(messages));
            }

            #endregion

            // Remove rights
            rightsRepository.Remove(rightsModel);
            messages.Add(new Message(MessageTypeEnum.Info,
                                     4005,
                                     new List <string>()
            {
                rightsModel.Name
            }));
            return(Ok(messages));
        }
示例#9
0
 /// <summary>
 /// Gets an access right by it's ID or null if no matching right could be found.
 /// </summary>
 /// <param name="id">ID of the access right to get.</param>
 /// <returns>Returns the access right or null if no matching right could be found.</returns>
 public Right GetRight(string id)
 {
     return(RightsRepository.GetRight(id));
 }
示例#10
0
 /// <summary>
 /// Gets all existing access rights.
 /// </summary>
 /// <returns>Returns all access rights.</returns>
 public IEnumerable <Right> GetAllRights()
 {
     return(RightsRepository.GetAllRights());
 }
示例#11
0
        public IActionResult Create([FromBody] RightsModel fromBodyRightsModel)
        {
            // List of messages to return to the client
            var messages = new List <Message>();

            // Authentication
            var controllerHelper = new ControllerHelper(context);
            var authUserModel    = controllerHelper.Authenticate(HttpContext.User.Identity as ClaimsIdentity);

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

            // Authorization
            if (!AuthorizationHelper.IsAuthorized(authUserModel, (long)SystemDatasetsEnum.Rights, RightsEnum.CR))
            {
                return(Forbid());
            }

            #region VALIDATIONS

            // New rights ApplicationId must be the same as of authorized user
            var sharedValidationHelper = new SharedValidationHelper();
            messages = sharedValidationHelper.ValidateApplicationId(fromBodyRightsModel.ApplicationId, authUserModel.ApplicationId);
            if (messages.Count != 0)
            {
                return(BadRequest(messages));
            }
            fromBodyRightsModel.Application = authUserModel.Application;

            // New rights must have a unique name
            var rightsRepository = new RightsRepository(context);
            var sameNameRights   = rightsRepository.GetByApplicationIdAndName(authUserModel.ApplicationId, fromBodyRightsModel.Name);
            if (sameNameRights.Count() > 0)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         4001,
                                         new List <string>()
                {
                    fromBodyRightsModel.Name
                }));
                return(BadRequest(messages));
            }

            // Rights data validity and logic validity
            messages = sharedValidationHelper.ValidateRights(authUserModel.Application.ApplicationDescriptor,
                                                             fromBodyRightsModel.DataDictionary);
            if (messages.Count != 0)
            {
                return(BadRequest(messages));
            }

            #endregion

            rightsRepository.Add(fromBodyRightsModel);
            messages.Add(new Message(MessageTypeEnum.Info,
                                     4002,
                                     new List <string>()
            {
                fromBodyRightsModel.Name
            }));
            return(Ok(messages));
        }
示例#12
0
        public IActionResult Create(string email, IFormFile file)
        {
            // List of messages to return to the client
            var messages = new List <Message>();

            #region application descriptor validations

            // File with JSON application descriptor is required
            if (file == null)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         0001,
                                         new List <string>()));
                return(BadRequest(messages));
            }

            // Get JObject from input file
            JObject applicationDescriptorJObject;
            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                // Try to parse file to JObject - only valid JSON files are parsed
                try
                {
                    applicationDescriptorJObject = JObject.Parse(reader.ReadToEnd());
                }
                // If parsing was unsuccessfull, return error message containing location of error
                catch (JsonReaderException e)
                {
                    messages.Add(new Message(MessageTypeEnum.Error,
                                             0002,
                                             new List <string>()
                    {
                        e.Message
                    }));
                    return(BadRequest(messages));
                }
            }
            var sharedAppInitHelper = new SharedAppInitHelper();

            // With successfully parsed JSON file, validate it against schema
            var schemaValidationMessages = sharedAppInitHelper.ValidateJSONAgainstSchema(applicationDescriptorJObject);
            // If validation JSON is not valid return errors
            if (schemaValidationMessages.Count != 0)
            {
                return(BadRequest(schemaValidationMessages));
            }
            // Get ApplicationDescriptor class instance from JObject
            var applicationDescriptor = applicationDescriptorJObject.ToObject <ApplicationDescriptor>();

            // LoginApplicationName must be unique
            var applicationRepository = new ApplicationRepository(context);
            var applicationModel      = applicationRepository.GetByLoginApplicationName(applicationDescriptor.LoginApplicationName);
            if (applicationModel != null)
            {
                messages.Add(new Message(MessageTypeEnum.Error,
                                         0003,
                                         new List <string>()
                {
                    applicationDescriptor.LoginApplicationName
                }));
            }
            // Validate datasets and attributes
            messages.AddRange(sharedAppInitHelper.ValidateDescriptor(applicationDescriptor));

            if (messages.Count != 0)
            {
                return(BadRequest(messages));
            }

            #endregion

            // Set default values to the application descriptor
            sharedAppInitHelper.SetDefaultDescriptorValues(applicationDescriptor);

            #region create new application

            using (var transaction = context.Database.BeginTransaction())
            {
                // Create new application and add it to the database
                var serializedApplicationDescriptor = JsonConvert.SerializeObject(applicationDescriptor);
                var newApplication = new ApplicationModel {
                    LoginApplicationName      = applicationDescriptor.LoginApplicationName,
                    ApplicationDescriptorJSON = serializedApplicationDescriptor
                };
                applicationRepository.Add(newApplication);

                // Create new admin account for the application
                // Random password
                string newPassword;
                var    minPasswordLength = applicationDescriptor.SystemDatasets.UsersDatasetDescriptor.PasswordAttribute.Min;
                if (minPasswordLength != null)
                {
                    newPassword = PasswordHelper.GenerateRandomPassword((int)minPasswordLength);
                }
                else
                {
                    newPassword = PasswordHelper.GenerateRandomPassword(Constants.MinSaferPasswordLength);
                }
                // Admin rights
                var appInitHelper    = new AppInitHelper();
                var newRights        = appInitHelper.GetAdminRights(newApplication, applicationDescriptor);
                var rightsRepository = new RightsRepository(context);
                rightsRepository.Add(newRights);
                var salt    = PasswordHelper.GetSalt();
                var newUser = new UserModel
                {
                    Application  = newApplication,
                    PasswordHash = PasswordHelper.ComputeHash(salt + newPassword),
                    PasswordSalt = salt,
                    Data         = appInitHelper.GetDefaultAdminDataDictionary(applicationDescriptor.SystemDatasets.UsersDatasetDescriptor),
                    Rights       = newRights,
                    Language     = applicationDescriptor.DefaultLanguage
                };
                var userRepository = new UserRepository(context);
                userRepository.Add(newUser);

                // Try to send login details to admin account to email from parametres
                try
                {
                    appInitHelper.SendEmailWithCredentials(email, applicationDescriptor.ApplicationName, newApplication.LoginApplicationName, newPassword);
                }
                catch
                {
                    messages.Add(new Message(MessageTypeEnum.Error,
                                             0025,
                                             new List <string>()
                    {
                        email
                    }));
                    return(BadRequest(messages));
                }

                // Commit all
                transaction.Commit();
            }
            // If everythong was ok, save changes to the database
            context.SaveChangesAsync();

            #endregion

            messages.Add(new Message(MessageTypeEnum.Info,
                                     0026,
                                     new List <string>()
            {
                applicationDescriptor.ApplicationName, email
            }));
            return(Ok(messages));
        }