public async Task <IHttpActionResult> PostRolePermissionMap(RolePermissionMap rolePermissionMap)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.RolePermissionMaps.Add(rolePermissionMap);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RolePermissionMapExists(rolePermissionMap.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = rolePermissionMap.Id }, rolePermissionMap));
        }
        public async Task <IHttpActionResult> PutRolePermissionMap(Guid id, RolePermissionMap rolePermissionMap)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != rolePermissionMap.Id)
            {
                return(BadRequest());
            }

            db.Entry(rolePermissionMap).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RolePermissionMapExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> DeleteConfirmed(Guid id)
        {
            RolePermissionMap rolePermissionMap = await db.RolePermissionMaps.FindAsync(id);

            db.RolePermissionMaps.Remove(rolePermissionMap);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task <IHttpActionResult> GetRolePermissionMap(Guid id)
        {
            RolePermissionMap rolePermissionMap = await db.RolePermissionMaps.FindAsync(id);

            if (rolePermissionMap == null)
            {
                return(NotFound());
            }

            return(Ok(rolePermissionMap));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,RoleId,PermissionId,StatusCodeId")] RolePermissionMap rolePermissionMap)
        {
            if (ModelState.IsValid)
            {
                db.Entry(rolePermissionMap).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.PermissionId = new SelectList(db.PermissionMasters, "Id", "Name", rolePermissionMap.PermissionId);
            ViewBag.RoleId       = new SelectList(db.RoleMasters, "Id", "Name", rolePermissionMap.RoleId);
            return(View(rolePermissionMap));
        }
        public async Task <IHttpActionResult> DeleteRolePermissionMap(Guid id)
        {
            RolePermissionMap rolePermissionMap = await db.RolePermissionMaps.FindAsync(id);

            if (rolePermissionMap == null)
            {
                return(NotFound());
            }

            db.RolePermissionMaps.Remove(rolePermissionMap);
            await db.SaveChangesAsync();

            return(Ok(rolePermissionMap));
        }
Exemplo n.º 7
0
        // GET: RolePermissionMaps/Details/5
        public async Task <ActionResult> Details(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            RolePermissionMap rolePermissionMap = await db.RolePermissionMaps.FindAsync(id);

            if (rolePermissionMap == null)
            {
                return(HttpNotFound());
            }
            return(View(rolePermissionMap));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Create([Bind(Include = "Id,RoleId,PermissionId,StatusCodeId")] RolePermissionMap rolePermissionMap)
        {
            if (ModelState.IsValid)
            {
                rolePermissionMap.Id = Guid.NewGuid();
                db.RolePermissionMaps.Add(rolePermissionMap);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.PermissionId = new SelectList(db.PermissionMasters, "Id", "Name", rolePermissionMap.PermissionId);
            ViewBag.RoleId       = new SelectList(db.RoleMasters, "Id", "Name", rolePermissionMap.RoleId);
            return(View(rolePermissionMap));
        }
        public async Task <Tuple <bool, string[]> > CreateRoleAsync(RoleDTO role, IEnumerable <int> claims)
        {
            ObjectMapper    mapper  = new ObjectMapper();
            ApplicationRole appRole = mapper.ConvertRoleToIdentityRole(role);

            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var result = await _appRoleManager.CreateAsync(appRole);

                    if (!result.Succeeded)
                    {
                        return(Tuple.Create(false, result.Errors.ToArray()));
                    }


                    appRole = await _appRoleManager.FindByNameAsync(appRole.Name);


                    if (claims != null)
                    {
                        List <RolePermissionMap> rolePermissionList = new List <RolePermissionMap>();

                        foreach (int claim in claims)
                        {
                            RolePermissionMap tmpDTO = new RolePermissionMap();
                            tmpDTO.PermissionId = claim;
                            tmpDTO.RoleId       = appRole.Id;

                            rolePermissionList.Add(tmpDTO);
                        }

                        _context.RolePermissionMaps.AddRange(rolePermissionList);
                        await _context.SaveChangesAsync();

                        dbContextTransaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                    throw ex;
                }
            }

            return(Tuple.Create(true, new string[] { appRole.Id }));
        }
Exemplo n.º 10
0
        // GET: RolePermissionMaps/Edit/5
        public async Task <ActionResult> Edit(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            RolePermissionMap rolePermissionMap = await db.RolePermissionMaps.FindAsync(id);

            if (rolePermissionMap == null)
            {
                return(HttpNotFound());
            }
            ViewBag.PermissionId = new SelectList(db.PermissionMasters, "Id", "Name", rolePermissionMap.PermissionId);
            ViewBag.RoleId       = new SelectList(db.RoleMasters, "Id", "Name", rolePermissionMap.RoleId);
            return(View(rolePermissionMap));
        }
        protected void save(RolePermissionMapDto dto)
        {
            if (dto.permissions.Count == 0)
            {
                throw new InvalidValueException("At least one permission is required.");
            }

            var role = _roleRepo.getById(dto.role_id) ?? throw new ItemNotFoundException($"User role with the id {dto.role_id} doesnot exist.");

            foreach (var permission in dto.permissions)
            {
                RolePermissionMap rolePermissionMap = new RolePermissionMap()
                {
                    role            = role,
                    permission_name = permission
                };
                _rolePermissionMapRepo.insert(rolePermissionMap);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Add a permission to a role
        /// </summary>
        /// <param name="roleSystemName">The role to add the permission to</param>
        /// <param name="permissionRecordSystemName">The permission to add to the role</param>
        public void AddPermissionToRole(string roleSystemName, string permissionRecordSystemName)
        {
            var role = _userRoleCollection.Find(Query<UserRole>.EQ(x => x.SystemName, roleSystemName)).SetFields(Fields<UserRole>.Include(x => x.Id)).FirstOrDefault();

            if(role == null)
                throw new NoodleException("The role " + roleSystemName + " doesn't exist.");

            var permissionRecord = _permissionPecordCollection.Find(Query<PermissionRecord>.EQ(x => x.SystemName, permissionRecordSystemName)).SetFields(Fields<UserRole>.Include(x => x.Id)).FirstOrDefault();

            if(permissionRecord == null)
                throw new NoodleException("The permission record " + permissionRecordSystemName + " doesn't exist.");

            var mapping = new RolePermissionMap
            {
                PermissionRecordId = permissionRecord.Id,
                UserRoleId = role.Id
            };

            _rolePermissionMapCollection.Update(Query.And(Query<RolePermissionMap>.EQ(x => x.PermissionRecordId, permissionRecord.Id), Query<RolePermissionMap>.EQ(x => x.UserRoleId, role.Id)),
                Update<RolePermissionMap>.Replace(mapping),
                UpdateFlags.Upsert);
        }