// GET: Channel
        public async Task <IActionResult> Index()
        {
            UserModel user = await UserManager.GetUserAsync(User);

            var list = _context.Channels.Include(c => c.UserAuthor).Where(c => c.UserAuthor.Id == user.Id).ToList();

            if (list.Count == 0)
            {
                ChannelModel channelModel = new ChannelModel();
                channelModel.Color      = "red";
                channelModel.Name       = $"{user.UserName}";
                channelModel.UserAuthor = user;

                _context.Channels.Add(channelModel);
                _context.SaveChanges();

                int newChanneID = _context.Channels.Where(c => c.Name == user.UserName).Single().ID;

                UserChannelModel userChannel = new UserChannelModel();
                userChannel.UserID    = user.Id;
                userChannel.ChannelID = newChanneID;

                _context.UserChannels.Add(userChannel);
                _context.SaveChanges();
            }

            var userOutput = _context.Users.Include(u => u.Channels).Where(u => u.Id == user.Id).Single();

            return(View(userOutput));
        }
        public async Task <IActionResult> Create([Bind("Name, Color")] ChannelModel channelModel)
        {
            UserModel user = await UserManager.GetUserAsync(User);

            var existChannel = _context.Channels.Where(c => c.Name == channelModel.Name).Include(c => c.UserAuthor).SingleOrDefault();

            if (ModelState.IsValid && (existChannel == null || existChannel.UserAuthor.UserName != user.UserName))
            {
                channelModel.UserAuthor = user;

                _context.Channels.Add(channelModel);
                await _context.SaveChangesAsync();

                channelModel = _context.Channels.Where(c => c.Name == channelModel.Name).Include(c => c.UserAuthor).Where(c => c.UserAuthor.UserName == user.UserName).Single();

                UserChannelModel userChannel = new UserChannelModel();
                userChannel.UserID    = user.Id;
                userChannel.ChannelID = channelModel.ID;

                _context.UserChannels.Add(userChannel);
                await _context.SaveChangesAsync();

                return(Redirect("/Channel/Details/" + channelModel.ID));
            }
            else
            {
                ViewBag.info = "kanał już istnieje!";
            }
            return(View(channelModel));
        }
Beispiel #3
0
        public async Task <ActionResult <bool> > DeleteMessage([FromBody] UserChannelModel model)
        {
            try
            {
                var token  = ExtractHeader(HttpContext, "Authorization", ',', 1);
                var claims = new List <BusinessModels.UserAccessControl.UserClaimModel>();
                claims.Add(new BusinessModels.UserAccessControl.UserClaimModel("Id", model.UserId.ToString()));


                var accessPolicy = _authorizationPolicyManager.ConfigureCustomPolicy(new List <string>()
                {
                    "message:delete"
                }, claims);

                if (!_authorizationResolutionManager.Authorize(token, accessPolicy))
                {
                    return(StatusCode(403));
                }
                await _messagingService.DeleteMessageAsync(model.ChannelId);

                return(Ok(true));
            }
            catch
            {
                return(Ok(false));
            }
        }
Beispiel #4
0
        public async Task <ActionResult <int> > GetChannelOwner([FromBody] UserChannelModel model)
        {
            try
            {
                var token  = ExtractHeader(HttpContext, "Authorization", ',', 1);
                var claims = new List <BusinessModels.UserAccessControl.UserClaimModel>();
                claims.Add(new BusinessModels.UserAccessControl.UserClaimModel("Id", model.UserId.ToString()));

                var accessPolicy = _authorizationPolicyManager.ConfigureCustomPolicy(new List <string>()
                {
                    "channels:getowner"
                }, claims);
                if (!_authorizationResolutionManager.Authorize(token, accessPolicy))
                {
                    return(StatusCode(403));
                }
                int ownerId = await _messagingService.GetChannelOwnerAsync(model.ChannelId);

                if (ownerId == 0)
                {
                    return(StatusCode(404));
                }

                return(Ok(ownerId));
            }
            catch
            {
                return(StatusCode(404));
            }
        }
Beispiel #5
0
        public ActionResult DeleteConfirmed(int id)
        {
            UserChannelModel userChannelModel = db.UserChannelModels.Find(id);

            db.UserChannelModels.Remove(userChannelModel);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Beispiel #6
0
 public ActionResult Edit([Bind(Include = "ID,UserID,ChannelID")] UserChannelModel userChannelModel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(userChannelModel).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(userChannelModel));
 }
Beispiel #7
0
        public ActionResult Create([Bind(Include = "ID,UserID,ChannelID")] UserChannelModel userChannelModel)
        {
            if (ModelState.IsValid)
            {
                db.UserChannelModels.Add(userChannelModel);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(userChannelModel));
        }
Beispiel #8
0
        // GET: UserChannel/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserChannelModel userChannelModel = db.UserChannelModels.Find(id);

            if (userChannelModel == null)
            {
                return(HttpNotFound());
            }
            return(View(userChannelModel));
        }
        public async Task <IActionResult> ShowAllChannel()
        {
            UserModel user = await UserManager.GetUserAsync(User);

            var list = _context.Channels.Include(c => c.UserAuthor).Where(c => c.UserAuthor.Id == user.Id).ToList();

            if (list.Count == 0)
            {
                ChannelModel channelModel = new ChannelModel();
                channelModel.Color      = "red";
                channelModel.Name       = $"{user.UserName}";
                channelModel.UserAuthor = user;

                _context.Channels.Add(channelModel);
                _context.SaveChanges();

                int newChanneID = _context.Channels.Where(c => c.Name == user.UserName).Single().ID;

                UserChannelModel userChannel = new UserChannelModel();
                userChannel.UserID    = user.Id;
                userChannel.ChannelID = newChanneID;

                _context.UserChannels.Add(userChannel);
                _context.SaveChanges();
            }


            var listChnnel = _context.Channels.Include(c => c.UserAuthor).ToList();

            if (listChnnel.Count == 0)
            {
                ViewBag.Info = "Brak kanałów";
                return(View());
            }

            return(View(listChnnel));
        }
Beispiel #10
0
        public async Task <ActionResult <IEnumerable <UserIdModel> > > GetAllUsersInGroup([FromBody] UserChannelModel model)
        {
            try
            {
                var token  = ExtractHeader(HttpContext, "Authorization", ',', 1);
                var claims = new List <BusinessModels.UserAccessControl.UserClaimModel>();
                claims.Add(new BusinessModels.UserAccessControl.UserClaimModel("Id", model.UserId.ToString()));

                var accessPolicy = _authorizationPolicyManager.ConfigureCustomPolicy(new List <string>()
                {
                    "messaging.users:get"
                }, claims);

                if (!_authorizationResolutionManager.Authorize(token, accessPolicy))
                {
                    return(StatusCode(403));
                }
                IEnumerable <UserIdModel> models = await _messagingService.GetAllUsersInChannelAsync(model.ChannelId);

                return(Ok(models));
            }
            catch
            {
                return(StatusCode(404));
            }
        }