Filter doesn't works in Codeiginter 4 - codeigniter-4

I have using framework Codeiginter 4 (learning).I am using filter for authenticating session but filter doesn't works.Can you help how to use filter in CI4.If i have not login need to load login page...without redirecting to that specific controller.
My Filter code:
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class Auth implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
$session = session();
if(!$session->has('logged_user')){
return redirect()->to('/Login');
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
At config\filters
public $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'auth' => \App\Filters\Auth::class,
'noauth' => \App\Filters\NoAuth::class,
];
Route.php
$routes->get('dashboard','Dashboard::index',['filter' =>'auth']);
$routes->get('role/index','Role::index',['filter' =>'auth']);
please help me to solve this problem.I have tired lot but not working for me

Related

Shopware 6 - Custom CMS elements: How to get the data from two repositories into a custom CMS block?

We are working on a custom shopware 6 plugin. This plugin contains a custom CMS block with two CMS elements. The CMS elements need data from two different custom repositories which are not associated to each other.
We tried to set up two DataResolver (one for each CMS element) but they are overwriting each other. So we tried to put everything in one DataResolver. We checked how some other plugins do this but it is not very clear to us.
Can someone help us what we need to put into the CMSElementReslover.php file especially into these two methods:
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
// Some other plugins return null here(?)
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
// If we return null at the collect method, our $result variable is empty.
// We need the two custom repositories here...
}
We setup two additional classes for each element which extends our CmsElementResolver, e.g. like this:
class ExampleOneCmsElementResolver extends CmsElementResolver
{
public function getType(): string
{
}
public function getStruct(): Struct
{
}
The documentation is not very detailed: https://developer.shopware.com/docs/guides/plugins/plugins/content/cms/add-data-to-cms-elements
Thanks for any hint! 🙏🏼
Best!
#shopware #shopware6
We tried to add both repositories to the collection, e.g. like this:
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$config = $slot->getFieldConfig();
$criteria = new Criteria();
$criteriaCollection = new CriteriaCollection();
$criteriaCollection->add(
Entity1Definition::ENTITY_NAME,
Entity1Definition::class,
$criteria
);
$criteria = new Criteria();
$criteriaCollection->add(
Entity2Definition::ENTITY_NAME,
Entity2Definition::class,
$criteria
);
return $criteriaCollection->all() ? $criteriaCollection : null;
}
You are on the right track with adding your criteria to the collection.
Let's say you want to fetch a media and a product entity.
In the collect method you simply provide a unique key with both your criteria.
In the enrich method you retrieve the result of the respective search with the same key you used for the criteria.
Then it's simply a matter of setting your entities you retrieve from the search results to the slot by using an instance of Struct, e.g. ArrayStruct.
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$mediaIdConfig = $slot->getFieldConfig()->get('mediaId');
$productIdConfig = $slot->getFieldConfig()->get('productId');
if ($mediaIdConfig === null || $productIdConfig === null) {
return null;
}
$criteriaCollection = new CriteriaCollection();
$criteria = new Criteria([$mediaIdConfig->getStringValue()]);
$criteriaCollection->add('media_' . $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
$criteria = new Criteria([$productIdConfig->getStringValue()]);
$criteriaCollection->add('product_' . $slot->getUniqueIdentifier(), ProductDefinition::class, $criteria);
return $criteriaCollection;
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$mediaSearchResult = $result->get('media_' . $slot->getUniqueIdentifier());
$productSearchResult = $result->get('product_' . $slot->getUniqueIdentifier());
if ($mediaSearchResult === null || $productSearchResult === null) {
return;
}
$slot->setData(new ArrayStruct([
'media' => $mediaSearchResult->first(),
'product' => $productSearchResult->first(),
]));
}

Unit testing that the swagger doc is correct without starting a server

I'd like to test that the swagger document is correct for my application (mainly, because I've added a strategy to generate custom OperationIds and I want to ensure they are correctly unique)
However, the only solutions I found are all using a "real" server (cf https://stackoverflow.com/a/52521454/1545567), which is not an option for me since I do not have the database, message bus, etc... when I launch the unit tests in CI...
At the moment, I have the following but it always generate 0 paths and 0 models ...
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using SampleCheckIn;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Linq;
using Xunit;
using SampleCheckIn.Def;
using Service.Utils;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
namespace D4Interop.Tests
{
public class TmpTest
{
[Fact]
public void Tmp()
{
var controllers = typeof(Startup).Assembly.GetTypes().Where(x => IsController(x)).ToList();
controllers.Any().Should().BeTrue();
var services = new ServiceCollection();
controllers.ForEach(c => services.AddScoped(c));
services.AddLogging(logging => logging.AddConsole());
services.AddControllers(); //here, I've also tried AddMvcCore and other ASP methods...
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("api", new OpenApiInfo { Title = Constants.SERVICE_NAME, Version = "_", Description = Constants.SERVICE_DESC });
//c.OperationFilter<SwaggerUniqueOperationId>(); //this is my filter that ensures the operationId is unique
c.CustomOperationIds(apiDesc =>
{
return apiDesc.TryGetMethodInfo(out var methodInfo) ? methodInfo.Name : null;
});
});
services.AddSingleton<IWebHostEnvironment>(new FakeWebHostEnvironment());
var serviceProvider = services.BuildServiceProvider();
var swaggerProvider = serviceProvider.GetRequiredService<ISwaggerProvider>();
var swagger = swaggerProvider.GetSwagger("api");
swagger.Should().NotBeNull();
swagger.Paths.Any().Should().BeTrue();
}
private bool IsController(Type x)
{
return typeof(Microsoft.AspNetCore.Mvc.ControllerBase).IsAssignableFrom(x);
}
}
internal class FakeWebHostEnvironment : IWebHostEnvironment
{
public FakeWebHostEnvironment()
{
}
public IFileProvider WebRootFileProvider { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string WebRootPath { get => "/root"; set => throw new NotImplementedException(); }
public string EnvironmentName { get => "dev"; set => throw new NotImplementedException(); }
public string ApplicationName { get => "app"; set => throw new NotImplementedException(); }
public string ContentRootPath { get => "/"; set => throw new NotImplementedException(); }
public IFileProvider ContentRootFileProvider { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
}
Ok, I've finally found that I just need to mix the linked answer with my code :
[Fact]
public async Task TestSwagger()
{
var server = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(options => { options.UseStartup<Startup>(); })
.Build();
var swagger = server.Services
.GetRequiredService<ISwaggerProvider>()
.GetSwagger("xxx"); //xxx should be the name of your API
swagger.Should().NotBeNull();
swagger.Paths.Any().Should().BeTrue();
swagger.Components.Schemas.Should().NotBeNull();
}

C# Bot Framework : Form flow set value for the field based on previous Answer [duplicate]

Hello I'm new to Microsoft Bot Framework and I have a question that I couldn't find an answer to.
I have a FormFlow that ask the user for some question, after a specific question I want the bot to do some logic and show messages accordingly (for example if the user selected option 1 then show message X and if the user selected option 2 show message Y).
Here is my code:
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bot_CRM.FormFlow
{
public enum RequestOptions { Unknown, CheckStatus, CreateCase };
[Serializable]
public class CaseFormFlow
{
public RequestOptions RequestType;
[Prompt("What is your first name?")]
public string FirstName;
public string LastName;
public string ContactNumber;
[Prompt("Please enter your id")]
public string Id;
public static IForm<CaseFormFlow> BuildForm()
{
OnCompletionAsyncDelegate<CaseFormFlow> processRequest = async (context, state) =>
{
await context.PostAsync($#"Thanks for your request");
};
return new FormBuilder<CaseFormFlow>()
.Message("Hello and welcom to my service desk bot")
.Field(nameof(FirstName))
.Message("hello {FirstName}")
.Field(nameof(Id))
.Field(nameof(RequestType)) =>
//here if user select 1 start flow of check status and if user select 2 start flow of create case
.AddRemainingFields()
.Message("Thank you request. Our help desk team will get back to you shortly.")
.OnCompletion(processRequest)
.Build();
}
}
}
Updated code after Ezequiel's suggestion:
return new FormBuilder<CaseFormFlow>()
.Message("Hello and welcom to my service desk bot")
.Field(nameof(FirstName))
.Message("hello {FirstName}")
.Field(new FieldReflector<CaseFormFlow>(nameof(RequestType))
.SetActive(state => state.AskUserForRequestType)
.SetNext((value, state) =>
{
var selection = (RequestOptions)value;
if (selection == RequestOptions.CheckStatus)
{
return new NextStep(new[] { nameof(Id) });
}
else
{
return new NextStep();
}
}))
Thanks in advance for the help
This is a great question.The key thing is to use the SetActive and SetNext methods of the Field<T> class. You should consider using the FieldReflector class; though you can implement your own IField.
SetActive is described in the Dynamic Fields section of the FormFlow documentation. Basically it provides a delegate that enables the field based on a condition.
SetNext will allow you to decide what step of the form should come next based on your custom logic.
You can take a look to the ContosoFlowers sample. In the Order form; something similar is being done.
public static IForm<Order> BuildOrderForm()
{
return new FormBuilder<Order>()
.Field(nameof(RecipientFirstName))
.Field(nameof(RecipientLastName))
.Field(nameof(RecipientPhoneNumber))
.Field(nameof(Note))
.Field(new FieldReflector<Order>(nameof(UseSavedSenderInfo))
.SetActive(state => state.AskToUseSavedSenderInfo)
.SetNext((value, state) =>
{
var selection = (UseSaveInfoResponse)value;
if (selection == UseSaveInfoResponse.Edit)
{
state.SenderEmail = null;
state.SenderPhoneNumber = null;
return new NextStep(new[] { nameof(SenderEmail) });
}
else
{
return new NextStep();
}
}))
.Field(new FieldReflector<Order>(nameof(SenderEmail))
.SetActive(state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.SetNext(
(value, state) => (state.UseSavedSenderInfo == UseSaveInfoResponse.Edit)
? new NextStep(new[] { nameof(SenderPhoneNumber) })
: new NextStep()))
.Field(nameof(SenderPhoneNumber), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.Field(nameof(SaveSenderInfo), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.Build();
}
}
}

Magento 1.8.1.0 Custom pagination not showing

I have work with Magento 1.8.1.0 Version, for my requirement i create a custom pagination.here the pagination link is not showing. in phtml file i added the line $this->getPagerHtml()
// model intiate
private function getModel() {
return Mage::getModel('retailers/retailersmodel');
}
// set the collection
public function __construct() {
parent::__construct();
$model = $this->getModel();
$collection = $model->getCollection();
$this->setCollection($collection);
}
//custom pagination code
protected function _prepareLayout() {
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array(5 => 5, 10 => 10, 20 => 20, 'all' => 'all'));
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml() {
return $this->getChildHtml('pager');
}
thanks for you idea and suggestion.

ASP.net Identity Disable User

Using the new ASP.net Identity in MVC 5, How do we disable a user from logging in? I don't want to delete them, maybe just disable their account for a time period.
Does anyone have any ideas on this as I don't see a status column or anything on the ASPNetUsers table.
await userManager.SetLockoutEnabledAsync(applicationUser.Id, true);
await userManager.SetLockoutEndDateAsync(DateTime.Today.AddYears(10));
Update: As CountZero points out, if you're using v2.1+, then you should try and use the lockout functionality they added first, before trying the solution below. See their blog post for a full sample: http://blogs.msdn.com/b/webdev/archive/2014/08/05/announcing-rtm-of-asp-net-identity-2-1-0.aspx
Version 2.0 has the IUserLockoutStore interface that you can use to lockout users, but the downside is that there is no OOB functionality to actually leverage it beyond the pass-through methods exposed by the UserManager class. For instance, it would be nice if it would actually increment the lockout count as a part of the standard username/password verification process. However, it's fairly trivial to implement yourself.
Step #1: Create a custom user store that implements IUserLockoutStore.
// I'm specifying the TKey generic param here since we use int's for our DB keys
// you may need to customize this for your environment
public class MyUserStore : IUserLockoutStore<MyUser, int>
{
// IUserStore implementation here
public Task<DateTimeOffset> GetLockoutEndDateAsync(MyUser user)
{
//..
}
public Task SetLockoutEndDateAsync(MyUser user, DateTimeOffset lockoutEnd)
{
//..
}
public Task<int> IncrementAccessFailedCountAsync(MyUser user)
{
//..
}
public Task ResetAccessFailedCountAsync(MyUser user)
{
//..
}
public Task<int> GetAccessFailedCountAsync(MyUser user)
{
//..
}
public Task<bool> GetLockoutEnabledAsync(MyUser user)
{
//..
}
public Task SetLockoutEnabledAsync(MyUser user, bool enabled)
{
//..
}
}
Step #2: Instead of UserManager, use the following class in your login/logout actions, passing it an instance of your custom user store.
public class LockingUserManager<TUser, TKey> : UserManager<TUser, TKey>
where TUser : class, IUser<TKey>
where TKey : IEquatable<TKey>
{
private readonly IUserLockoutStore<TUser, TKey> _userLockoutStore;
public LockingUserManager(IUserLockoutStore<TUser, TKey> store)
: base(store)
{
if (store == null) throw new ArgumentNullException("store");
_userLockoutStore = store;
}
public override async Task<TUser> FindAsync(string userName, string password)
{
var user = await FindByNameAsync(userName);
if (user == null) return null;
var isUserLockedOut = await GetLockoutEnabled(user);
if (isUserLockedOut) return user;
var isPasswordValid = await CheckPasswordAsync(user, password);
if (isPasswordValid)
{
await _userLockoutStore.ResetAccessFailedCountAsync(user);
}
else
{
await IncrementAccessFailedCount(user);
user = null;
}
return user;
}
private async Task<bool> GetLockoutEnabled(TUser user)
{
var isLockoutEnabled = await _userLockoutStore.GetLockoutEnabledAsync(user);
if (isLockoutEnabled == false) return false;
var shouldRemoveLockout = DateTime.Now >= await _userLockoutStore.GetLockoutEndDateAsync(user);
if (shouldRemoveLockout)
{
await _userLockoutStore.ResetAccessFailedCountAsync(user);
await _userLockoutStore.SetLockoutEnabledAsync(user, false);
return false;
}
return true;
}
private async Task IncrementAccessFailedCount(TUser user)
{
var accessFailedCount = await _userLockoutStore.IncrementAccessFailedCountAsync(user);
var shouldLockoutUser = accessFailedCount > MaxFailedAccessAttemptsBeforeLockout;
if (shouldLockoutUser)
{
await _userLockoutStore.SetLockoutEnabledAsync(user, true);
var lockoutEndDate = new DateTimeOffset(DateTime.Now + DefaultAccountLockoutTimeSpan);
await _userLockoutStore.SetLockoutEndDateAsync(user, lockoutEndDate);
}
}
}
Example:
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Login(string userName, string password)
{
var userManager = new LockingUserManager<MyUser, int>(new MyUserStore())
{
DefaultAccountLockoutTimeSpan = /* get from appSettings */,
MaxFailedAccessAttemptsBeforeLockout = /* get from appSettings */
};
var user = await userManager.FindAsync(userName, password);
if (user == null)
{
// bad username or password; take appropriate action
}
if (await _userManager.GetLockoutEnabledAsync(user.Id))
{
// user is locked out; take appropriate action
}
// username and password are good
// mark user as authenticated and redirect to post-login landing page
}
If you want to manually lock someone out, you can set whatever flag you're checking in MyUserStore.GetLockoutEnabledAsync().
You can have a new class, which should be derived from IdentityUser class. YOu can add a boolean property in the new class and can use this new property of take care per check for login process. I also done it pretty well. I might wanna take a look at : blog
UserManager.RemovePasswordAsync("userId") will effectively disable a user. If the user has no password he will not be able to log in. You will need to set a new password to enable the user again.

Resources