Here is a code I have got:
[AbpAuthorize(AppPermissions.Pages_Administration_JobLocations_Manage)]
public async Task<JobLocationDto> UpdateJobLocation(UpdateJobLocationInput input)
{
var jobLocation = await _jobLocationRepository.GetAsync(input.Id);
jobLocation.DisplayName = input.DisplayName;
await _jobLocationRepository.UpdateAsync(jobLocation);
//await EntityFrameworkCore.Repositories.RepositoryExtensions.UpdateOwnAsync(_jobLocationRepository, jobLocation);
return ObjectMapper.Map<JobLocationDto>(jobLocation);
}
The commented section works fine when uncommented but I wonder if there is any way of having the UpdateOwnAsync method instead of the UpdateAsync method on the _jobLocationRepository?
Is there any method to check if a request is coming from an Auhenticated user? Something such as IsUserAuthenticated....?
- Use the AbpAuthorize attribute
I know about the attribute but I have got a method which is available to both authenticated and unauthenticated users with 1 different behavior for each scenario:
public async Task<ListResultDto<JobLocationDto>> GetJobLocationList(GetJobLocationListInput input)
{
if (await _userManager.IsGrantedAsync(GetCurrentUser().Id, AppPermissions.Pages_Administration_JobLocations_Manage))
{
var jobLocations = await _jobLocationRepository.GetAll().Where(jobLocation => input.State == Common.VisibilityState.All ? (jobLocation.IsActive || !jobLocation.IsActive) :
input.State == Common.VisibilityState.Active ? jobLocation.IsActive : !jobLocation.IsActive).ToListAsync();
return new ListResultDto<JobLocationDto>(ObjectMapper.Map<List<JobLocationDto>>(jobLocations));
}
else
{
var jobLocations = await _jobLocationRepository.GetAll().Where(jobLocation => jobLocation.IsActive).ToListAsync();
return new ListResultDto<JobLocationDto>(ObjectMapper.Map<List<JobLocationDto>>(jobLocations));
}
}
The problem is that the IsGrantedAsync() fails and throws exception when no logged in user. So first I need to check if the request is coming from a logged in user or not
A new question: Is there any method to check if a request is coming from an Auhenticated user? Something such as IsUserAuthenticated....?
Thanks Aaron,
<cite>aaron: </cite> A helper like this can be implemented as an extension:
namespace AbpCompanyName.AbpProjectName.Domain.Repositories { public static class RepositoryExtensions { public static Task<TEntity> UpdateOwnAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, TEntity entity) where TEntity : class, IEntity<TPrimaryKey>, ICreationAudited { var currentUserId = SingletonDependency<IAbpSession>.Instance.UserId; if (currentUserId != entity.CreatorUserId) { return null; // Or throw exception } return repository.UpdateAsync(entity); } } }
Usage:
// using AbpCompanyName.AbpProjectName.Domain.Repositories _repository.UpdateOwnAsync(entity);
That should be sufficient for you to modify it yourself if you want to:
- pass in "Own" as a parameter instead of the method name, or
- use it with updateAction.
Thanks Aaron, Just a couple of quick questions: Where should I place this RepositoryExtension? I put it in the RepositoryBase.cs file in the EntityFrameworkCore project. I didn't get what the _repository is in your usage sample! I doubt it'd be the RepositoryExtension class as you have defined it static. Here is how I am trying to use it:
public async Task<JobLocationDto> UpdateJobLocation(UpdateJobLocationInput input)
{
var jobLocation = await _jobLocationRepository.GetAsync(input.Id);
jobLocation.DisplayName = input.DisplayName;
await EntityFrameworkCore.Repositories.RepositoryExtensions.UpdateOwnAsync(_jobLocationRepository, jobLocation);
return ObjectMapper.Map<JobLocationDto>(jobLocation);
}
Is it right? Anyway that I could have the UpdateOwnAsync method in the normal repository list of methods rather than in another extension? Can I have a sample of how to use it with the upateAction parameter?
thanks a lot in advanced :)
Thanks! that was it! :D
Thanks Ibrahim. Is it possible to use multi tenancy enabled for my case and treat each application as a separate tenant? If yes, then I would have one Admin area per tenant, right? Do you recommend having multiple applications combined into one solution? All of these applications kind of communicate to each other.
Any further update on this ticket?
Hey Ibrahim, Thanks for your response. Yes that is exactly what I am looking for. Just not quite sure how to break the existing Authentication and Authorisation functions and entities into 2 or more pieces? Besides that, I was thinking of having one unified admin area to manage users and roles and other bits related... So perhaps my first wonder is if I can use the template that ASP Zero provides or I need to do it all from the scratch?
Hi Payman, Any chance that you share your solution of switching calendars Eng<>Persian? What Angular component did you end up using for the DatePicker? I have already found this [https://github.com/fingerpich/jalali-angular-datepicker]) but it is a bit buggy!
Thanks in advance ;)
Thanks for your comment. I have got kind confused on when/how I can use Metronic components in my Angular 2 app?! I have downloaded the whole Metronic package from the provided link (1 GB). I found out that there is no component for Angular 2 in the package. So, does it mean that having the Metronic package means nothing, unless the project is in JQUERY or AngularJS? Can I use the Metronic components directy without having their Angular 2 equivalents? In the angular-cli.js I noticed the followings:
"../src/assets/bootstrap-datepicker/js/bootstrap-datepicker.min.js",
"../node_modules/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js",
"../node_modules/bootstrap-daterangepicker/daterangepicker.js"
But couldn't locate their usage anywhere in the solution! On the other hand, there are 2 components for date-time-picker for Angular 2 in the app\shared\common\timing folders as of date-time.component.ts and date-range-picker.component.ts which I can use. Is there any relevancy between the 2 Angular components and the above mentioned .js files in the angular-cli.js? I have actually commented the inclusion of .js files out in the angular-cli.js and didn't notice a change where the date-time.component are used! What is the app.js file in the assets\metronic\script folder? Do I need it? Can I modify it?
So, I am kind of lost in understanding the usage of Metronic components in the Angular 2 application and would appreciate any tip and comment on any above wonders! thanks