Пример #1
0
    public IActionResult /*UserViewData*/ Login([FromBody] LoginData login_data)
    {
        var item = _context.User.FirstOrDefault(t => t.Username == login_data.Username || t.Email == login_data.Email);

        if (item != null)
        {
            var last_login_attempt = item.LastLoginAttempt;
            item.LastLoginAttempt = DateTime.Now;
            _context.Update(item);
            _context.SaveChanges();
            if (login_data.Password != null && (last_login_attempt != null || (DateTime.Now - last_login_attempt).TotalSeconds > 3))
            {
                if (PasswordHasher.CheckHash(login_data.Password, new PasswordAndSalt()
                {
                    PasswordHash = item.PasswordHash, PasswordSalt = item.PasswordSalt
                }))
                {
                    HttpContext.Login <LoggableEntities, User>(env, _context, "User", item, new LoggableEntities()
                    {
                        User = item
                    });

                    return(Ok(UserViewData.FromUser(item)));
                }
            }
        }
        return(Unauthorized());
    }
Пример #2
0
    public IActionResult /*UserViewData*/ Create()
    {
        var session       = HttpContext.Get <LoggableEntities>(_context);
        var current_User  = session == null ? null : session.User;
        var current_Admin = session == null ? null : session.Admin;

        if (_context.User.Any(u => u.Username == null || u.Email == null || u.Username == "" || u.Email == ""))
        {
            return(Unauthorized());
        }
        // throw new Exception("Unauthorized create attempt");
        var can_create_by_token = ApiTokenValid || true;

        if (!can_create_by_token)
        {
            return(Unauthorized());
        }
        // throw new Exception("Unauthorized create attempt");
        var item = new User()
        {
            CreatedDate = DateTime.Now, Id = _context.User.Max(i => i.Id) + 1
        };

        _context.User.Add(PortableRecipes.Models.User.FilterViewableAttributesLocal(current_User, current_Admin)(item));
        _context.SaveChanges();
        item = PortableRecipes.Models.User.WithoutImages(item);
        return(Ok(UserViewData.FromUser(item)));
    }
Пример #3
0
    public UserViewData Register([FromBody] RegistrationData registration_data)
    {
        string username           = registration_data.Username,
               email              = registration_data.Email,
               email_confirmation = registration_data.EmailConfirmation;

        if (username != null && username != "" && email != null && email != "" && email == email_confirmation)
        {
            var item = _context.User.FirstOrDefault(t => t.Username == username || t.Email == email);
            if (item == null)
            {
                var new_password_text = PasswordHasher.RandomPassword;
                var new_password      = PasswordHasher.Hash(new_password_text);
                item = new User()
                {
                    Id = _context.User.Max(i => i.Id) + 1, Username = username, Email = email, PasswordHash = new_password.PasswordHash, PasswordSalt = new_password.PasswordSalt
                };
                var apiKey           = StaticMailer._mailOptions.MailApiToken;
                var client           = new SendGridClient(apiKey);
                var from             = new EmailAddress(StaticMailer._mailOptions.MailFrom);
                var subject          = "User account created with temporary password.";
                var to               = new EmailAddress(item.Email);
                var plainTextContent = $"Your User temporary password has set. Your username and password combination is \n\nUsername: {item.Username}\nPassword: {new_password_text}\n";
                var htmlContent      = $"Your User temporary password has set. Your username and password combination is <br />Username: {item.Username}<br />Password: {new_password_text}<br />";
                var msg              = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
                var response         = client.SendEmailAsync(msg).Result;

                _context.User.Add(item);
                _context.SaveChanges();

                return(UserViewData.FromUser(item));
            }
        }
        throw new Exception("Cannot register.");
    }
Пример #4
0
    public IActionResult /*UserViewData*/ GetUser_RatingById(int Rating_id, int User_id)
    {
        var session           = HttpContext.Get <LoggableEntities>(_context);
        var current_User      = session == null ? null : session.User;
        var current_Admin     = session == null ? null : session.Admin;
        var allowed_sources   = ApiTokenValid ? _context.Rating : _context.Rating;
        var source            = allowed_sources.FirstOrDefault(s => s.Id == Rating_id);
        var can_view_by_token = ApiTokenValid || true;

        if (source == null || !can_view_by_token)
        {
            return(NotFound());
        }
        var allowed_targets = ApiTokenValid ? _context.User : _context.User;
        var item            = (from link in _context.User_Rating
                               where link.RatingId == source.Id
                               from target in allowed_targets
                               where link.UserId == target.Id
                               select target).OrderBy(i => i.CreatedDate)
                              .Select(PortableRecipes.Models.User.FilterViewableAttributes(current_User, current_Admin))
                              .FirstOrDefault(t => t.Id == User_id);

        if (item == null)
        {
            return(NotFound());
        }
        item = PortableRecipes.Models.User.WithoutImages(item);
        return(Ok(UserViewData.FromUser(item)));
    }
Пример #5
0
    public IActionResult /*ItemWithEditable<UserViewData>*/ GetById(int id)
    {
        var session        = HttpContext.Get <LoggableEntities>(_context);
        var current_User   = session == null ? null : session.User;
        var current_Admin  = session == null ? null : session.Admin;
        var allowed_items  = ApiTokenValid ? _context.User : _context.User;
        var editable_items = ApiTokenValid ? _context.User : current_Admin != null || current_User != null ? (current_User != null ? (from _User in _context.User where _User.Id == current_User.Id
                                                                                                                                      select _User) : _context.User) : Enumerable.Empty <User>().AsQueryable();
        var item_full = allowed_items.FirstOrDefault(e => e.Id == id);

        if (item_full == null)
        {
            return(NotFound());
        }
        var item = PortableRecipes.Models.User.FilterViewableAttributesLocal(current_User, current_Admin)(item_full);

        item = PortableRecipes.Models.User.WithoutImages(item);
        return(Ok(new ItemWithEditable <UserViewData>()
        {
            Item = UserViewData.FromUser(item),
            Editable = editable_items.Any(e => e.Id == item.Id)
        }));
    }
Пример #6
0
    public IActionResult /*IEnumerable<UserViewData>*/ CreateNewUser_Rating_User(int Rating_id)
    {
        var session             = HttpContext.Get <LoggableEntities>(_context);
        var current_User        = session == null ? null : session.User;
        var current_Admin       = session == null ? null : session.Admin;
        var allowed_sources     = ApiTokenValid ? _context.Rating : _context.Rating;
        var source              = allowed_sources.FirstOrDefault(s => s.Id == Rating_id);
        var can_create_by_token = ApiTokenValid || true;

        if (source == null || !can_create_by_token)
        {
            return(Unauthorized());
        }
        // throw new Exception("Cannot create item in relation User_Ratings");
        var can_link_by_token = ApiTokenValid || true;

        if (!CanAdd_Rating_User_Ratings(source) || !can_link_by_token)
        {
            return(Unauthorized());
        }
        //throw new Exception("Cannot add item to relation User_Ratings");
        var new_target = new User()
        {
            CreatedDate = DateTime.Now, Id = _context.User.Max(i => i.Id) + 1
        };

        _context.User.Add(new_target);
        _context.SaveChanges();
        var link = new User_Rating()
        {
            Id = _context.User_Rating.Max(l => l.Id) + 1, RatingId = source.Id, UserId = new_target.Id
        };

        _context.User_Rating.Add(link);
        _context.SaveChanges();
        return(Ok(new UserViewData[] { UserViewData.FromUser(new_target) }));
    }
Пример #7
0
    public Page <UserViewData> GetAll([FromQuery] int page_index, [FromQuery] int page_size = 25)
    {
        var session        = HttpContext.Get <LoggableEntities>(_context);
        var current_User   = session == null ? null : session.User;
        var current_Admin  = session == null ? null : session.Admin;
        var allowed_items  = ApiTokenValid ? _context.User : _context.User;
        var editable_items = ApiTokenValid ? _context.User : current_Admin != null || current_User != null ? (current_User != null ? (from _User in _context.User where _User.Id == current_User.Id
                                                                                                                                      select _User) : _context.User) : Enumerable.Empty <User>().AsQueryable();
        var can_edit_by_token   = ApiTokenValid || true;
        var can_create_by_token = ApiTokenValid || true;
        var can_delete_by_token = ApiTokenValid || true;
        var items = allowed_items.OrderBy(i => i.CreatedDate).AsQueryable();

        return(items
               .Select(PortableRecipes.Models.User.FilterViewableAttributes(current_User, current_Admin))
               .Select(s => Tuple.Create(s, can_edit_by_token && editable_items.Any(es => es.Id == s.Id)))
               .Paginate(can_create_by_token, can_delete_by_token, false, page_index, page_size, PortableRecipes.Models.User.WithoutImages, item => UserViewData.FromUser(item), null));
    }
Пример #8
0
    public Page <UserViewData> GetUser_Ratings(int Rating_id, [FromQuery] int page_index, [FromQuery] int page_size = 25)
    {
        var session             = HttpContext.Get <LoggableEntities>(_context);
        var current_User        = session == null ? null : session.User;
        var current_Admin       = session == null ? null : session.Admin;
        var allowed_sources     = ApiTokenValid ? _context.Rating : _context.Rating;
        var source              = allowed_sources.FirstOrDefault(s => s.Id == Rating_id);
        var can_create_by_token = ApiTokenValid || true;
        var can_delete_by_token = ApiTokenValid || true || true;
        var can_link_by_token   = ApiTokenValid || true;
        var can_view_by_token   = ApiTokenValid || true;

        if (source == null || !can_view_by_token)                   // test
        {
            return(Enumerable.Empty <PortableRecipes.Models.User>() // B
                   .AsQueryable()
                   .Select(PortableRecipes.Models.User.FilterViewableAttributes(current_User, current_Admin))
                   .Select(t => Tuple.Create(t, false))
                   .Paginate(can_create_by_token, can_delete_by_token, can_link_by_token, page_index, page_size, PortableRecipes.Models.User.WithoutImages, item => UserViewData.FromUser(item), null));
        }
        var allowed_targets  = ApiTokenValid ? _context.User : _context.User;
        var editable_targets = ApiTokenValid ? _context.User : (current_Admin != null || current_User != null ? (current_User != null ? (from _User in _context.User where _User.Id == current_User.Id
                                                                                                                                         select _User) : _context.User) : Enumerable.Empty <User>().AsQueryable());
        var can_edit_by_token = ApiTokenValid || true;
        var items             = (from link in _context.User_Rating
                                 where link.RatingId == source.Id
                                 from target in allowed_targets
                                 where link.UserId == target.Id
                                 select target).OrderBy(i => i.CreatedDate).AsQueryable();

        return(items
               .Select(PortableRecipes.Models.User.FilterViewableAttributes(current_User, current_Admin))
               .Select(t => Tuple.Create(t, can_edit_by_token && editable_targets.Any(et => et.Id == t.Id)))
               .Paginate(can_create_by_token, can_delete_by_token, can_link_by_token, page_index, page_size, PortableRecipes.Models.User.WithoutImages, item => UserViewData.FromUser(item), null));
    }