How to bulk import users into userfrosting? - userfrosting

what is the best way to bulk import users into userfrosting?
I tried importing CSV into uf_users table with flag_verified and flag_enabled set to 1, with requisite fields (id, user_name, display_name, password)
While data shows up in admin interface, individual users are not able to login.

** Solution provided by Alex (#alexw) **
Thanks to the create of UF Alex (#alexw), further to the discussion at https://chat.userfrosting.com/channel/support, here is how import was solved - as suggested by Alex.
Add relevant rows to other related tables -
uf_user_event with fields event = 'sign_up', occured_at = timestamp, description = 'user xyz successfully registered on...'
uf_group_user with fields user_id = with id of imported user, group_id = 1 (that is default)
With this, the imported user is able to login!
Thanks Alex!

Related

How to select by fields in preloaded object?

I wonder if it is possible to select by a condition in a preloaded object. For example, I have tables User and Profile (one to one). So I need to get all Users with Profiles where sex is female.
I thought it can be done by something like this:
Preload("UserProfile").Where("user_profile.sex = ?", "female")
But it returns something like:
pq: missing FROM-clause entry for table \"user_profile\"
Preloading doesn't join the tables specified. You need to explicitly join the table in question:
Preload("UserProfile").Joins("LEFT JOIN user_profile ON user.id = user_profile.user_id")
.Where("user_profile.sex = ?", "female")
...Assuming your primary key is called id, and foreign key is called user_id.
I also faced the same issue in a recent project. I found the below solution to work for me.
var users []User
Preload("UserProfile","sex = ?","female").Find(&users)
and then check for
user.profile!=nil
The issue in this approach is it will load all users.
But in your case, it can be another way around.
var profiles []Profile
Preload("User").where("sex = ?","female").Find(&profiles)
I hope this will solve your problem.

Customize Passport Queries time of authenticating

For every request I found that 4 queries are fired to validate the user and the token. Among them one is to fetch the user (select * from user) based on the user id. This queries are fired by Passport/Laravel But what I want is to modify this query to add one status field check also to check if any user become invalid during the token validity period. If we only check with the id then if any user become inactive(By changing status then also we will not be able to stop the user as deleting the token for the user is not a good solution for me).
Queries Fired on every request by Passport Laravel:
select * from oauth_access_tokens where id = ?
select * from user where id = ? limit 1 ["2"]
select * from oauth_access_tokens where id = ?
select * from oauth_clients where id = ?
So, can anyone tell me how to change the 'select * from user where id' query in passport at time of Token validation.
You can add this method on your User model (or any model you're authenticating with passport)
...
public function findForPassport($username)
{
return $user = (new self)->where('email', $username)->where('is_active', 1)->first();
}
...
of course you can modify is_active by whichever column you are using (and/or any query constraint for that matter), as long as it returns Illuminate\Contracts\Auth\Authenticatable contract.
I wouldn't try and modify passports default behaviour as I have no idea what else it might impact both now and in future upgrades.
Your best bet might be to hook into the passport events and apply you business logic to a listener that is called when the events are fired

Use ldap3 to query all active directory groups a user belongs to

I have no problem to query the domain user from active directory with this query
from ldap3 import ObjectDef, AttrDef, Reader, Entry, Attribute, OperationalAttribute
import ldap3
person = ObjectDef('inetOrgPerson')
s = ldap3.Server('myad.com')
c = ldap3.Connection(s, user = 'myuser', password = 'mypassword')
ldap3.Reader(c, person, '(&(objectCategory=person)(sAMAccountName=myuser))', 'dc=mydomain,dc=com').search()
however, this query returns empty list of groups the user belongs to, how to make it work?
ldap3.Reader(c, person, '(&(objectCategory=group)(member=myuser))', 'dc=mydomain,dc=com').search()
I use ldap3. Thanks in advance.
This answer helped me:
I need the full DN of the user returned from the first query, so this works:
ldap3.Reader(c, person, '(&(member=CN=myuser_in_full_name,OU=xxx,OU=xxxxxx,DC=mydomain,DC=com)(objectClass=group))', 'dc=mydomain,dc=com').search()

sqlalchemy Oracle schema

I should extract data from an oracle database.
How can I find out which schema are defined in the database?
When I do not define any schema in the description of Metadata(), I find no tables.
thanks for your help,
Default Oracle schema matches the username that was used in Oracle connection.
If you don't see any tables - it means the tables are created in another schema.
Looks like you have two questions here:
1) about Oracle schemas - how to find schema and tables in Oracle
2) about SQLAlchemy reflections - how to specify Oracle schema for table
You can find answer for the first question in many places. I.e. here: https://stackoverflow.com/a/2247758/1296661
Answering second question:
Table class constructor has schema argument to specify table's schema if it is different from default user's schema. See more here
http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#sqlalchemy.schema.Table
Here is the python code to answer second question. You will need to setup db connection and table name values to match your case:
from sqlalchemy import Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
engine = create_engine('oracle://<user_name>:<password>#<hostname>:1521/<instance name>', echo=True)
Base = declarative_base()
reflected_table = Table('<Table name>',
Base.metadata,
autoload=True,
autoload_with=engine,
schema='<Schema name other then user_name>')
print [c.name for c in reflected_table.columns]
p = engine.execute("SELECT OWNER,count(*) table_count FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'TABLE' GROUP BY OWNER");
for r in p:
print r
Good luck with using sqlschema and reflection feature - it is a lot of fun. You get your python program working with existing database almost without defining schema information in your program.
I'm using this feature with oracle db in production - the only thing I have to define were relations between tables explicitly setting foreign and primary keys.
I found two other ways to set an alternate Oracle schema.
Using MetaData and passing it into the creation of the SqlAlchemy instance or engine.
Or using __table_args__ when defining your models and pass in {'schema': 'other_schema' }
http://docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.MetaData.params.schema
How to specify PostgreSQL schema in SQLAlchemy column/foreign key mixin?
But both of these don't help the foreign key calls. For those, you still have to manually prefix with other_schema. before the table name.
The snippet below is in the context of a Flask app with Flask-SqlAlchemy in use.
from sqlalchemy import schema
from flask_sqlalchemy import SQLAlchemy
import os
os.environ['TNS_ADMIN'] = os.path.join( 'oracle_tools', 'network', 'admin')
oracle_connection_string = 'oracle+cx_oracle://{username}:{password}#{tnsname}'.format(
username='user',
password='pass',
tnsname='TNS_SPACE',
)
oracle_schema = 'user_schema_1'
foreign_key_prefix = oracle_schema + '.'
app.config['SQLALCHEMY_DATABASE_URI'] = oracle_connection_string
oracle_db_metadata = schema.MetaData(schema=oracle_schema)
db = SQLAlchemy(app, metadata=oracle_db_metadata)
user_groups_table = db.Table(db_table_prefix + 'user_groups', db.Model.metadata,
db.Column('...', db.Integer, db.ForeignKey(foreign_key_prefix + 'group.id')),
# schema=oracle_schema # Not necessary because of the use of MetaData above
)
class User(db.Model):
__tablename__ = 'user'
# __table_args__ = {'schema': oracle_schema } # Not necessary because of the use of MetaData above
user_name = db.Column('USER_NAME', db.String(250))
And... two other related links:
https://github.com/mitsuhiko/flask-sqlalchemy/issues/172
How to specify PostgreSQL schema in SQLAlchemy column/foreign key mixin?
Hope that helps.

With the use of Apex code, how can I select all ''OpportunityProducts' objects which are belongs to particular 'Opportunity Id'

I am way to customizing 'Sales' application that belongs to 'salesforce.com' platform.
Is there any way to select all the 'OpportunityProducts' objects which are belongs to particular 'Opportunity Id' ?
[SELECT Id FROM OpportunityProduct WHERE Opportunity =:opportunitId];
When I execute above code for select those 'OpportunityProduct', I got following error. If any one have some idea please update me. Thanks.
Save error: sObject type 'OpportunityProduct' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.
Another way to get this done when you need the actual products, not just the line items, is as follows. First get your opportunities:
List<Opportunity> opps = [SELECT Id, Name FROM Opportunity LIMIT 1000];
Then loop through to create a list of opportunity Ids
List<Id> oppIds = new List<Id>();
for(Opportunity o : opps)
{
oppIds.add(o.Id);
}
Now get your actual products that belong to your opportunities...
List<OpportunityLineItem> oppProds = [SELECT Id, PricebookEntry.Product2.Name, PricebookEntry.Product2.Family
FROM OpportunityLineItem
WHERE OpportunityId IN :oppIds];
Hope that helps.

Resources