Hi Everyone,
I am trying to create a rest application that provides rest APIs for my UI (regardless of whether it’s built in angular, react or vue). I simply executed the below command in commandbox.
coldbox create app name=restapi skeleton=rest
This created a rest service applicaton with some of the handlers and APIs already included. and login handler is one of it.
originally these handlers related to login and authorization are using a User.cfc that extends the User.cfc from cbsecurity module.
Below is the code for that (or you can refer to this one GitHub - lmajano/hmvc-presso-demo: The demo application for the Hierarchical MVC presentation on ColdBox , which is a little different than the app that get created by the command but almost similar to it)
/**
* Copyright since 2016 by Ortus Solutions, Corp
* www.ortussolutions.com
* ---
* This is a basic user object that can be used with CBSecurity.
*
* It implements the following interfaces via it's delegates
* - cbsecurity.interfaces.jwt.IJwtSubject
* - cbsecurity.interfaces.IAuthUser
*/
component
accessors ="true"
transientCache="false"
delegates ="
Auth@cbSecurity,
Authorizable@cbSecurity,
JwtSubject@cbSecurity
"
{
/**
* --------------------------------------------------------------------------
* Properties
* --------------------------------------------------------------------------
*/
property name="id";
property name="firstName";
property name="lastName";
property name="username";
property name="password";
property name="permissions";
property name="roles";
/**
* --------------------------------------------------------------------------
* Validation constraints
* --------------------------------------------------------------------------
* https://coldbox-validation.ortusbooks.com/overview/valid-constraints
*/
this.constraints = {
firstName : { required : true, size : "1..255" },
lastName : { required : true, size : "1..255" },
username : { required : true, size : "1..255" },
password : { required : true, size : "1..255" }
};
/**
* --------------------------------------------------------------------------
* Validation profiles
* --------------------------------------------------------------------------
* https://coldbox-validation.ortusbooks.com/overview/validating-constraints/validating-with-profiles
*/
this.constraintProfiles = { "update" : "firstName,lastName,username" };
/**
* --------------------------------------------------------------------------
* Mementifier Serialization
* --------------------------------------------------------------------------
* https://forgebox.io/view/mementifier
*/
this.memento = {
// Default properties to serialize
defaultIncludes : [
"id",
"firstName",
"lastName",
"username",
"permissions",
"roles"
],
// Default Exclusions
defaultExcludes : [],
// Never Include
neverInclude : [ "password" ]
};
/**
* --------------------------------------------------------------------------
* Population Control
* --------------------------------------------------------------------------
* https://coldbox.ortusbooks.com/readme/release-history/whats-new-with-7.0.0#population-enhancements
*/
this.population = {
include : [], // if empty, tries to include them all
exclude : [ "permissions", "roles" ] // These are not mass assignable
}
/**
* Constructor
*/
function init(){
variables.id = "";
variables.firstName = "";
variables.lastName = "";
variables.username = "";
variables.password = "";
variables.permissions = [];
variables.roles = [];
return this;
}
/**
* Set roles into the object
*
* @roles array or list of roles
*/
User function setRoles( roles ){
if ( isSimpleValue( arguments.roles ) ) {
arguments.roles = listToArray( arguments.roles );
}
variables.roles = arguments.roles;
return this;
}
/**
* Set permissions into this object
*
* @permissions array or list of permissions
*/
User function setPermissions( permissions ){
if ( isSimpleValue( arguments.permissions ) ) {
arguments.permissions = listToArray( arguments.permissions );
}
variables.permissions = arguments.permissions;
return this;
}
/**
* Verify if this is a valid user or not
*/
boolean function isLoaded(){
return ( !isNull( variables.id ) && len( variables.id ) );
}
}
I am trying to modify some of the functions in the UserService (hmvc-presso-demo/models/UserService.cfc at master · lmajano/hmvc-presso-demo · GitHub) , to use the User data from database instead of the mockuser (mockuser follows the same pattern as above user model). Now the problem is I am getting “An exception ocurred: The getId method was not found.” whenever I am trying to run the login handler.
I guess the jwtAuth().attempt( rc.username, rc.password ); is still referring to the above mentioned User and it’s properties. Is there any way to customize it so the jwtAuth doesn’t refer to the user properties mentioned in modules\cbsecurity\models\auth\User.cfc.
Can I modify this file to match the model of User data in DB or should I leave it as it is since it’s a part of the cbsecurity library.
Any suggestion/advice is helpful and most welcome.